import { Button, Image } from '@tarojs/components';
import Taro from '@tarojs/taro';

import { Dialog, Popup } from '@taroify/core';
import { Fragment, useCallback, useState } from 'react';

import JobBuy from '@/components/product-dialog/steps-ui/job-buy';
import SafeBottomPadding from '@/components/safe-bottom-padding';
import { PageUrl } from '@/constants/app';
import { CacheKey } from '@/constants/cache-key';
import { GET_CONTACT_TYPE } from '@/constants/job';
import { navigateTo, switchTab } from '@/utils/route';
import './index.less';

interface IProps {
  onCancel: () => void;
  isCreateResume?: boolean;
  onConfirm: (type: GET_CONTACT_TYPE) => void;
}

const PREFIX = 'prejob-popup';

const GET_CONTACT_TYPE_OPTIONS = [
  {
    type: GET_CONTACT_TYPE.MATERIAL,
    icon: 'https://publiccdn.neighbourhood.com.cn/img/file.svg',
    title: '创建模卡（免费报单）',
    desc: '免费报单，优先推荐给企业，机会更多',
    btnText: '创建',
  },
  {
    type: GET_CONTACT_TYPE.VIP,
    icon: 'https://publiccdn.neighbourhood.com.cn/img/diamond.svg',
    title: '播络会员',
    desc: '开通会员每天免费查看',
    btnText: '开通',
  },
  // {
  //   type: GET_CONTACT_TYPE.GROUP,
  //   icon: 'https://publiccdn.neighbourhood.com.cn/img/group-avatar.png',
  //   title: '进群领会员（免费报单）',
  //   desc: '群内定期发放会员，免费报单',
  //   btnText: '进群',
  // },
  {
    type: GET_CONTACT_TYPE.INVITE,
    icon: 'https://publiccdn.neighbourhood.com.cn/img/invite-operations.png',
    title: '邀请运营进群（送会员）',
    desc: '每邀请进一个主播群送一个日会员',
    btnText: '邀请',
  },
];

export function PrejobPopup({ onCancel, isCreateResume, onConfirm }: IProps) {
  const [openPopup, setOpenPopup] = useState(true);
  const [openDialog, setOpenDialog] = useState(false);
  const [clicked, setClicked] = useState(!!Taro.getStorageSync(CacheKey.JOIN_GROUP_POPUP_CLICKED));
  const handleClick = (type: GET_CONTACT_TYPE) => () => {
    if (type === GET_CONTACT_TYPE.MATERIAL) {
      navigateTo(PageUrl.MaterialUploadVideo);
      onConfirm(type);
    }
    if (type === GET_CONTACT_TYPE.INVITE) {
      navigateTo(PageUrl.InviteOperations);
      onConfirm(type);
    }
    if (type === GET_CONTACT_TYPE.VIP) {
      setOpenPopup(false);
      setOpenDialog(true);
    }
    if (type === GET_CONTACT_TYPE.GROUP) {
      Taro.setStorageSync(CacheKey.JOIN_GROUP_POPUP_CLICKED, true);
      setClicked(true);
      switchTab(PageUrl.GroupV2);
      onConfirm(type);
    }
  };
  const handleAfterBuy = useCallback(async () => {
    onConfirm(GET_CONTACT_TYPE.VIP);
  }, [onConfirm]);
  return (
    <Fragment>
      <Popup rounded className={PREFIX} placement="bottom" open={openPopup} onClose={onCancel}>
        <div className={`${PREFIX}__content`}>
          <div className={`${PREFIX}__title`}>以下方式任选其一均可获取联系方式</div>
          <div className={`${PREFIX}__body`}>
            {GET_CONTACT_TYPE_OPTIONS.map(option => {
              if (clicked && option.type === GET_CONTACT_TYPE.GROUP) {
                return;
              }
              return (
                <div className={`${PREFIX}__item`} key={option.type}>
                  <div className={`${PREFIX}__item-icon ${option.type}`}>
                    <Image mode="aspectFit" src={option.icon} />
                  </div>
                  <div className={`${PREFIX}__item-main`}>
                    <div className="title">{option.title}</div>
                    <div className="desc">{option.desc}</div>
                  </div>
                  <div className={`${PREFIX}__item-action`}>
                    <Button className={`${PREFIX}__btn`} onClick={handleClick(option.type)}>
                      {option.btnText}
                    </Button>
                  </div>
                </div>
              );
            })}
          </div>
        </div>
        <SafeBottomPadding />
      </Popup>
      <Dialog open={openDialog} onClose={onCancel}>
        <Dialog.Content>
          <JobBuy onConfirm={handleAfterBuy} isCreateResume={isCreateResume} />
        </Dialog.Content>
      </Dialog>
    </Fragment>
  );
}
