import Taro, { useLoad } from '@tarojs/taro';

import { Button } from '@taroify/core';
import { useCallback } from 'react';

import HomePage from '@/components/home-page';
import { PageUrl } from '@/constants/app';
import { copy, logWithPrefix } from '@/utils/common';
import { navigateTo } from '@/utils/route';

import './index.less';

const PREFIX = 'dev-debug';
const log = logWithPrefix(PREFIX);
const marginTopStyle = { marginTop: 20 };

export default function DevDebug() {
  // const [lastOrderNo, setLastOrderNo] = useState<string>('');
  const handleLogin = useCallback(() => {
    Taro.login({
      success: res => {
        log('login code:', res.code);
        copy(res.code);
      },
      fail: e => log('login fail', e),
    });
  }, []);

  // const handleGetPhoneNumber = useCallback((e: BaseEventOrig<ButtonProps.onGetPhoneNumberEventDetail>) => {
  //   log(
  //     'handleGetPhoneNumber',
  //     `code: ${e.detail.code}`,
  //     `\niv: ${e.detail.iv}`,
  //     `\nencryptedData:${e.detail.encryptedData}`
  //   );
  //   const encryptedData = e.detail.encryptedData;
  //   const iv = e.detail.iv;
  //   if (!encryptedData || !iv) {
  //     Toast.error('取消授权');
  //     return;
  //   }
  //   Taro.setClipboardData({
  //     data: `code: ${e.detail.code}, iv: ${e.detail.iv}, encryptedData:${e.detail.encryptedData}`,
  //   });
  // }, []);

  // const handleCopyAndContact = async () => {
  //   openCustomerServiceChat();
  //   await copy('测试复制');
  // };

  // const handleGetLocationInfo = () => {
  //   requestLocation();
  // };

  // const handleCreateOrder = async (type: OrderType) => {
  //   try {
  //     const { payOrderNo, createPayInfo: payInfo } = await requestCreatePayInfo({
  //       type,
  //       amt: 1,
  //       productCode: type === OrderType.Group ? ProductType.AddGroup : ProductType.BossVip,
  //       productSpecId: type === OrderType.Group ? ProductSpecId.AddGroup1 : ProductSpecId.BossVip,
  //     });
  //     log('handlePay data', payOrderNo, payInfo);
  //     const res = await Taro.requestPayment({
  //       timeStamp: payInfo.timeStamp,
  //       nonceStr: payInfo.nonceStr,
  //       package: payInfo.packageVal,
  //       signType: payInfo.signType,
  //       paySign: payInfo.paySign,
  //     });
  //     setLastOrderNo(payOrderNo);
  //     log('handleBuy requestPayment res', res);
  //   } catch (e) {
  //     Toast.error('出错了，请重试');
  //     log('handleBuy error', e);
  //   }
  // };

  // const handleGetOrder = async () => {
  //   if (!lastOrderNo) {
  //     return;
  //   }
  //   try {
  //     const result = await requestOrderInfo({
  //       payOrderNo: lastOrderNo,
  //     });
  //     Taro.showToast({ title: JSON.stringify(result), icon: 'none' });
  //     log('handleGetOrder data', result);
  //   } catch (e) {
  //     Toast.error('出错了，请重试');
  //     log('handleGetOrder error', e);
  //   }
  // };

  const handleJumpPage = () => navigateTo(PageUrl.CertificationStart);

  // const handleSubscribeJob = async () => {
  //   const result = await subscribeMessage([SubscribeTempId.SUBSCRIBE_JOB]);
  //   log('handleSubscribeMessage result', result);
  //   Toast.info(`订阅结果: ${result[SubscribeTempId.SUBSCRIBE_JOB]}`);
  // };

  // const handleSubscribeVip = async () => {
  //   const result = await subscribeMessage([SubscribeTempId.SUBSCRIBE_VIP]);
  //   log('handleSubscribeMessage result', result);
  //   Toast.info(`订阅结果: ${result[SubscribeTempId.SUBSCRIBE_VIP]}`);
  // };

  // const handleQiniu = async () => {
  //   try {
  //     const media = await chooseMedia();
  //     if (!media) {
  //       return;
  //     }
  //     const { tempFiles } = media;
  //     const tempFile = tempFiles[0];
  //     if (!tempFile) {
  //       throw new Error('tempFile is not exist');
  //     }
  //     const onProgress: UploadTask.OnProgressUpdateCallback = res => {
  //       log('上传视频进度', res.progress, '总长度', res.totalBytesExpectedToSend, '已上传的长度', res.totalBytesSent);
  //       Taro.showLoading({ title: `上传${res.progress}%` });
  //     };
  //     await qiniuUpload.upload(tempFile.tempFilePath, onProgress);
  //   } catch (e) {
  //     console.error('upload fail', e);
  //     Toast.error('上传失败');
  //   } finally {
  //     Taro.hideLoading();
  //   }
  // };

  useLoad(() => {
    console.log('Page loaded.');
  });

  return (
    <HomePage>
      <div className={PREFIX}>
        {/* <div>{`最近一次的订单 ID: ${lastOrderNo}`}</div>
        <Button onClick={() => handleCreateOrder(OrderType.Group)} style={marginTopStyle} color="primary">
          下单群
        </Button>
        <Button onClick={() => handleCreateOrder(OrderType.BossVip)} style={marginTopStyle} color="primary">
          下单 BossVip
        </Button>
        <Button onClick={handleGetOrder} style={marginTopStyle} color="primary">
          查询最近一次订单
        </Button> */}
        {/* <Button onClick={() => navigateTo(PageUrl.JobPublish)} style={marginTopStyle} color="primary">
          跳转到通告发布
        </Button> */}
        <Button onClick={handleJumpPage} style={marginTopStyle} color="primary">
          跳转到开发页面
        </Button>
        {/* <Button onClick={handleSubscribeJob} style={marginTopStyle} color="primary">
          订阅通告推送
        </Button>
        <Button onClick={handleSubscribeVip} style={marginTopStyle} color="primary">
          订阅主播会员推送
        </Button> */}
        {/* <Button onClick={handleQiniu} style={marginTopStyle} color="primary">
          上传到七牛
        </Button> */}
        <Button onClick={handleLogin} style={marginTopStyle} color="primary">
          获取登录 code
        </Button>
      </div>
    </HomePage>
  );
}
