import React, {
  useState,
  useImperativeHandle,
  useEffect,
  forwardRef,
  memo,
} from "react";
import { Button, Input, View, Text, Image } from "@tarojs/components";
import Taro from "@tarojs/taro";
import classnames from "classnames";
import CommonPopup from "../CommonPopup";
import { useUserActions, useUserInfo } from "@/store/userStore";
import { EvaluateCallback, EvaluateScene } from "@/store/evaluateStore";
import { useNtrpLevels } from "@/store/pickerOptionsStore";
import NTRPTestEntryCard from "../NTRPTestEntryCard";
import NtrpPopupGuide from "../NTRPPopupGuide";
import Picker from "../Picker/Picker";
import CloseIcon from "@/static/ntrp/ntrp_popup_close.svg";
import styles from "./index.module.scss";

export enum EvaluateType {
  EDIT = "edit",
  EVALUATE = "evaluate",
}

export enum DisplayConditionType {
  AUTO = "auto",
  ALWAYS = "always",
}

export enum SceneType {
  LIST = "list",
  PUBLISH = "publish",
  PERSONAL = "personal",
  DETAIL = "detail",
}

interface NTRPEvaluatePopupProps {
  // types: EvaluateType[];
  // displayCondition: DisplayConditionType;
  // scene: SceneType;
  showGuide: boolean;
  type: EvaluateScene;
  // children: React.ReactNode;
}

// function showCondition(scene, ntrp) {
//   if (scene === "list") {
//     // TODO: 显示频率
//     return Math.random() < 0.1 && [0, undefined].includes(ntrp);
//   }
//   return true;
//   return !ntrpLevels.includes(ntrp);
// }

const NTRPEvaluatePopup = (props: NTRPEvaluatePopupProps, ref) => {
  const {
    // types = ["edit", "evaluate"],
    // displayCondition = "auto",
    // scene = "list",
    type,
    showGuide = false,
  } = props;
  const [visible, setVisible] = useState(false);
  const [ntrp, setNtrp] = useState<string>("1.5");
  const [guideShow, setGuideShow] = useState(() => showGuide);
  const { updateUserInfo } = useUserActions();
  const userInfo = useUserInfo();
  const ntrpLevels = useNtrpLevels();
  const [evaCallback, setEvaCallback] = useState<EvaluateCallback>({
    type: "",
    next: () => {},
    onCancel: () => {},
  });

  useImperativeHandle(ref, () => ({
    show: (evaluateCallback) => {
      setVisible(true);
      setEvaCallback(evaluateCallback);
    },
  }));

  // function handleEvaluate() {
  //   setVisible(false);
  //   // TODO: 实现NTRP评估逻辑
  //   Taro.navigateTo({
  //     url: `/other_pages/ntrp-evaluate/index?redirect=${encodeURIComponent(
  //       getCurrentFullPath()
  //     )}`,
  //   });
  // }

  // 当弹窗打开或用户信息变化时，从用户信息中提取并更新 ntrp 状态
  useEffect(() => {
    if (visible) {
      if (userInfo?.ntrp_level) {
        // 从 ntrp_level 中提取数字部分（如 "2.5" 或 "NTRP 2.5"）
        const match = String(userInfo.ntrp_level).match(/-?\d+(\.\d+)?/);
        if (match) {
          setNtrp(match[0]);
        } else {
          setNtrp("1.5");
        }
      } else {
        setNtrp("1.5");
      }
    }
  }, [visible, userInfo?.ntrp_level]);

  // const showEntry =
  //   displayCondition === "auto"
  //     ? showCondition(scene, ntrp)
  //     : displayCondition === "always";

  function handleClose() {
    console.log("hide  ....");
    setVisible(false);
    setGuideShow(showGuide);
  }

  async function handleChangeNtrp() {
    Taro.showLoading({ title: "修改中" });
    // 更新用户信息，会自动更新 store 中的 ntrp_level
    await updateUserInfo({ ntrp_level: ntrp });
    Taro.hideLoading();
    Taro.showToast({
      title: "NTRP水平修改成功",
      icon: "none",
    });
    evaCallback.next({ flag: true, score: String(ntrp) });
    handleClose();
  }

  return (
    <>
      <CommonPopup
        title="NTRP评估"
        visible={visible}
        onClose={handleClose}
        showHeader={false}
        hideFooter
        enableDragToClose={false}
      >
        {guideShow ? (
          <NtrpPopupGuide
            close={() => handleClose()}
            skipGuide={() => {
              setGuideShow(false);
            }}
            evaluateCallback={evaCallback}
          />
        ) : (
          <View className={styles.container}>
            <View className={styles.header}>
              <Text>选择 NTRP 自评水平</Text>
              <View className={styles.closeBtn} onClick={handleClose}>
                <Image className={styles.closeIcon} src={CloseIcon} />
              </View>
            </View>
            <View className={styles.entryCard}>
              <NTRPTestEntryCard type={type} evaluateCallback={evaCallback} />
            </View>
            <View className={styles.picker}>
              {/* FIXME: 有异常渲染问题 */}
              {visible && (
                <Picker
                  visible
                  options={ntrpLevels}
                  defaultValue={[ntrp]}
                  onChange={(val) => {
                    console.log(val[0]);
                    if (val[0]?.value) {
                      setNtrp(val[0]?.value as string);
                    }
                  }}
                />
              )}
            </View>
            <View className={styles.actions}>
              <View className={styles.buttonWrap}>
                <Button className={styles.button} onClick={handleClose}>
                  取消
                </Button>
              </View>
              <View className={styles.buttonWrap}>
                <Button
                  className={classnames(styles.button, styles.primary)}
                  onClick={handleChangeNtrp}
                >
                  保存
                </Button>
              </View>
            </View>
          </View>
        )}
      </CommonPopup>
    </>
  );
};

export default memo(forwardRef(NTRPEvaluatePopup));
