import React, {Component} from 'react'
import {observer} from "mobx-react";
import {createRoot} from "react-dom/client";
import store from "./store/store";

import "./core/checkwebp.ts";
import "./MD";
import './App.less'
import '@csstools/normalize.css';

import musicStore from "@/store/musicStore.ts";
import {initWx} from "@/built-in/share/weixin/weixin.ts";
import { ModalCtrl } from "@/core/ctrls/ModalCtrl.tsx";
import { PageCtrl } from "@/core/ctrls/PageCtrl.tsx";
import { GetCurrSkinId, getCustomShareId, getUrlParam, isMiniprogram, waitTime } from "@/utils/utils.ts";

import MyPrize from "@/pages/MyPrize/MyPrize.jsx";
import LoadingDemo from "@/pages/LoadingDemo/LoadingDemo.tsx";

import GamePage from "@/pages/GamePage/GamePage.tsx";
import HomePage from './pages/HomePage/HomePage.tsx';
import AgentSharepage from './pages/agentSharepage/sharepage.jsx';
import SharePage from './pages/sharepage/sharepage.jsx';
import { onInitShare, onUpdateShare } from './utils/share.js';
import { domain, ensureDomain } from "@spark/dbdomain";
import shareStore from './store/share.js';

import music from "../src/assets/audio/bgm.mp3";
// import * as eruda from "eruda";

// eruda.init({});

@observer
class App extends Component {

  showDefaultPage = () => {
    const skinId = GetCurrSkinId() || getCustomShareId();
    const defaultPage = {
      prize: MyPrize, // TODO 举例子 新宿台奖品页
      index: HomePage,
      sharepage:SharePage,
    }[skinId] || HomePage;
    PageCtrl.changePage(CFG.agentShare == "1" ?  AgentSharepage : defaultPage);
  }

  async componentDidMount() {
    const skinId = GetCurrSkinId() || getCustomShareId();
    console.info('skinId',skinId)
    if(CFG.agentShare != "1" && skinId == 'index'){
      musicStore.playSound( music, true);
    }

    this.showDefaultPage();
    await store.getFrontVariable();
    await onInitShare();
    await ensureDomain();
    CFG.domain = domain;
    if(isMiniprogram()){ //小程序域名写死
      CFG.domain = "https://96094-2-activity.dexfu.cn";
    }
    // await waitTime(0)
    // shareStore.initShare()
    setTimeout(()=>{
      const shareInfo = JSON.parse(store.frontVariable.shareInfo);
      if (skinId == 'index') { // 首页
        console.info('首页');
        let shareUrl = `${CFG.domain}${CFG.shareUrl}`;
        shareUrl += `&ykCode=${CFG.workerCode || CFG.agentCode || ""}`
        shareUrl += `&channel=${CFG.defaultChannel}&agentCode=${CFG.agentCode || ""}&actPlatforms=${CFG.actPlatforms || ""}&actId=${CFG.actId || ""}`
        onUpdateShare({
          title: shareInfo?.title,
          content: shareInfo?.content,
          thumbnail: shareInfo?.thumbnail,
          url: `${CFG.domain}${CFG.indexLogin}${encodeURIComponent(shareUrl)}`
        });
      } else {
        console.info('非首页');
        const inviteCode = getUrlParam("inviteCode");
        let shareUrl = `${CFG.domain}${CFG.shareUrl}${getUrlParam("inviteCode") ? '&inviteCode=' + getUrlParam("inviteCode") : ""}`;
        shareUrl += `&ykCode=${CFG.workerCode || CFG.agentCode || ""}`
        if (CFG.channel) shareUrl += `&channel=${CFG.defaultChannel}&agentCode=${CFG.agentCode || ""}&actPlatforms=${CFG.actPlatforms || ""}&actId=${CFG.actId || ""}`
        onUpdateShare({
          title: inviteCode && inviteCode != '' ? shareInfo?.titleInvite : shareInfo?.title,
          content: inviteCode && inviteCode != '' ? shareInfo?.contentInvite : shareInfo?.content,
          thumbnail: shareInfo?.thumbnail,
          url: `${CFG.domain}${CFG.indexLogin}${encodeURIComponent(shareUrl)}`
        });
      }
    },0)
  }

  componentWillUnmount() {
  }

  render() {
    return <>
        <PageCtrl/>
        <ModalCtrl/>
    </>;
  }
}

const root = createRoot(document.getElementById('root')!);
root.render(
  <App/>
);

