import Taro from '@tarojs/taro';

import { Dialog } from '@taroify/core';
import { useCallback, useEffect, useRef, useState } from 'react';

import { DialogStatus, PREFIX } from '@/components/product-dialog/const';
import QrCodeContent from '@/components/product-dialog/steps-ui/common-qr-code';
import CompanyPublishJobBuy from '@/components/product-dialog/steps-ui/company-publish-job-buy';
import PublishJobBuy from '@/components/product-dialog/steps-ui/publish-job-buy';
import { PageUrl } from '@/constants/app';
import { ProductType, QrCodeType } from '@/constants/product';
import { UserInfo } from '@/types/user';
import { requestProductBalance } from '@/utils/product';
import { navigateTo } from '@/utils/route';
import Toast from '@/utils/toast';

import '../index.less';

interface IProps {
  userInfo: UserInfo;
  onClose: (opened: boolean) => void;
  open?: boolean;
}

export default function PublishJobDialog(props: IProps) {
  const { userInfo, onClose } = props;
  const [status, setStatus] = useState<DialogStatus>(DialogStatus.LOADING);
  const initRef = useRef(() => {});

  const handleCloseDialog = useCallback(() => {
    onClose(false);
  }, [onClose]);

  const handleNext = useCallback(async () => {
    setStatus(DialogStatus.PUBLISH_QR_CODE);
  }, []);

  useEffect(() => {
    initRef.current = async () => {
      // if (1 < 2) {
      //   setStatus(DialogStatus.PUBLISH_QR_CODE);
      //   return;
      // }
      try {
        Taro.showLoading();
        const isVip = userInfo.isBoss && !userInfo.userBoss?.isExpire;
        if (isVip) {
          setStatus(DialogStatus.PUBLISH_QR_CODE);
        } else {
          setStatus(DialogStatus.PUBLISH_JOB_BUY);
        }
      } catch (e) {
        Toast.error('出错了，请重试');
        handleCloseDialog();
      } finally {
        Taro.hideLoading();
      }
    };
  }, [userInfo, handleCloseDialog]);

  useEffect(() => {
    initRef.current();
  }, []);

  if (status === DialogStatus.LOADING) {
    return null;
  }

  return (
    <Dialog className={PREFIX} onClose={onClose} open>
      <Dialog.Content>
        {status === DialogStatus.PUBLISH_JOB_BUY && <PublishJobBuy onNext={handleNext} />}
        {status === DialogStatus.PUBLISH_QR_CODE && <QrCodeContent type={QrCodeType.PublishJob} />}
      </Dialog.Content>
    </Dialog>
  );
}

export function PublishJobQrCodeDialog(props: Omit<IProps, 'userInfo'>) {
  const { onClose, open = true } = props;

  return (
    <Dialog className={PREFIX} onClose={onClose} open={open}>
      <Dialog.Content>
        <QrCodeContent type={QrCodeType.PublishJob} />
      </Dialog.Content>
    </Dialog>
  );
}

export function CompanyPublishJobDialog(props: IProps) {
  const { userInfo, onClose } = props;
  const [status, setStatus] = useState<DialogStatus>(DialogStatus.LOADING);
  const initRef = useRef(() => {});

  const handleCloseDialog = useCallback(() => {
    onClose(false);
  }, [onClose]);

  const handleNext = useCallback(async () => {
    onClose(true);
    navigateTo(PageUrl.JobPublish);
  }, [onClose]);

  useEffect(() => {
    initRef.current = async () => {
      // if (1 < 2) {
      //   setStatus(DialogStatus.COMPANY_PUBLISH_JOB_BUY);
      //   return;
      // }
      try {
        const productCode = ProductType.CompanyPublishJob;
        Taro.showLoading();
        const [time] = await requestProductBalance(productCode);
        if (time <= 0) {
          setStatus(DialogStatus.COMPANY_PUBLISH_JOB_BUY);
          return;
        }
        // 之前购买的次数还没用完
        handleNext();
      } catch (e) {
        Toast.error('出错了，请重试');
        handleCloseDialog();
      } finally {
        Taro.hideLoading();
      }
    };
  }, [userInfo, handleCloseDialog, handleNext]);

  useEffect(() => {
    initRef.current();
  }, []);

  if (status === DialogStatus.LOADING) {
    return null;
  }

  return (
    <Dialog className={PREFIX} onClose={onClose} open>
      <Dialog.Content>
        {status === DialogStatus.COMPANY_PUBLISH_JOB_BUY && <CompanyPublishJobBuy onNext={handleNext} />}
      </Dialog.Content>
    </Dialog>
  );
}
