// import { Component, PropsWithChildren } from 'react'

// class App extends Component<PropsWithChildren> {

//   componentDidMount () {}

//   componentDidShow () {}

//   componentDidHide () {}

//   render () {
//     // this.props.children 是将要会渲染的页面
//     return this.props.children
//   }
// }

// export default App

import { PropsWithChildren } from "react";
import { useRequest } from "ahooks";
import { Provider as ReduxProvider } from "react-redux";
import Taro, { useDidShow } from "@tarojs/taro";
import { WebView } from "@tarojs/components";
import StorageUtil, { EStorage } from "@/utils/storageUtil";
import AppContext from "./context/appContext";
import ReduxUtil from "./utils/reduxUtil";
import RouteUtil from "./utils/routeUtil";
import UserHelper from "./helper/userHelper";
import "./app.scss";

interface IAppProps {}

const App = (props: PropsWithChildren<IAppProps>) => {
  const result = useRequest(Taro.getSystemInfo);
  useRequest(() => {
    return UserHelper.cacheTabMenu();
  });
  useDidShow((options: any) => {
    console.log("This is Options", options);

    let jumpUrls = "";
    if (options.query?.source) {
      switch (options.query?.source) {
        case "app": {
          if (options.query?.platform) {
            switch (options.query?.platform) {
              case "cyqb":
                jumpUrls = "https://mp.weixin.qq.com/s/TBP2VhzYRaMWf_CWbo5mBA";
                break;
              case "hrqb":
                jumpUrls = "https://mp.weixin.qq.com/s/-E0vWsvT3bMMX34zkva_LQ";
                break;
              default:
                // 默认惠融钱包的文章
                jumpUrls = "https://mp.weixin.qq.com/s/-E0vWsvT3bMMX34zkva_LQ";
                break;
            }
          }
          break;
        }
        case "gxh": {
          jumpUrls = "https://mp.weixin.qq.com/s/RvsITI6t7_T-2UFC2pJvww";
          break;
        }
        case "wallet": {
          jumpUrls = "https://mp.weixin.qq.com/s/M-lkx1MTMRv2p7v-6ZFykg";
          break;
        }
      }
      RouteUtil.toWebViewPage({
        url: jumpUrls,
      });
    } else if (options.query?.openUrl) {
      RouteUtil.toWebViewPage({
        url: options.query?.openUrl,
      });
    }
    // if (options.query?.source === "app" && options.query?.platform) {
    //   let jumpUrls = "";

    //   switch (options.query?.platform) {
    //     case "cyqb":
    //       jumpUrls = "https://mp.weixin.qq.com/s/TBP2VhzYRaMWf_CWbo5mBA";
    //       break;
    //     case "hrqb":
    //       jumpUrls = "https://mp.weixin.qq.com/s/JJrWlgaA1lvIBAp7eNZjxg";
    //       break;
    //     default:
    //       // 默认惠融钱包的文章
    //       jumpUrls = "https://mp.weixin.qq.com/s/JJrWlgaA1lvIBAp7eNZjxg";
    //       break;
    //   }

    // }

    if (StorageUtil.get(EStorage.channelCode)) {
      StorageUtil.remove(EStorage.channelCode);
    }

    if (options.query?.channelCode) {
      StorageUtil.set(EStorage.channelCode, options.query?.channelCode);
    }

    const channelCode = extractChannelCodeValue(options.query);

    if (channelCode) {
      StorageUtil.set(EStorage.channelCode, channelCode);
    }
  });

  const extractChannelCodeValue = (query: any) => {
    for (let key in query) {
      if (key.includes("?channelCode")) {
        return query[key];
      }
    }
    return null;
  };

  return (
    // (appContext.systemInfo?.screenHeight || 0) - (appContext.systemInfo?.safeArea?.bottom || 0);
    <AppContext.Provider
      value={{
        systemInfo: result.data,
        screenSize: {
          width: result.data?.screenWidth || 0,
          height: result.data?.screenHeight || 0,
        },
        padding: {
          // top: result.data?.safeArea?.top || 0,
          top: 0,
          bottom:
            (result.data?.screenHeight || 0) -
            (result.data?.safeArea?.bottom || 0),
        },
      }}
    >
      <WebView src="https://mp.weixin.qq.com/s/4umQqPbPGfCgXqCizeXqKA"></WebView>
      <ReduxProvider store={ReduxUtil.store}>{props.children}</ReduxProvider>
    </AppContext.Provider>
  );
};

export default App;
