import PageCustom from "@/components/page-custom/index";
import { View, Image } from "@tarojs/components";
import { useEffect, useState } from "react";
import ComponentList from "@/components/component-list";

import NavBarNormal from "@/components/NavBarNormal/index";
import Taro, { getCurrentPages, useRouter } from "@tarojs/taro";
import { isSuccess } from "@/utils";

import IconArrowLeft from "@/components/icon/icon-arrow-left";
import IconHomeOutline from "@/components/icon/IconHomeOutline";
import SummaryBar from "@/components/AgentPage/components/SummaryBar";
import { useAgentStore } from "@/store/agentStore";
import style from "./index.module.less";
import { sceneUnzip } from "@/service/wechat";
import { postLog } from "./profile";

export default function Profile() {
  const { agentProfile, fetchAgentProfile } = useAgentStore();
  const params = useRouter().params;

  // 解码
  const decodeSceneValue = async (sceneValue: string) => {
    const response = await sceneUnzip(sceneValue);
    if (isSuccess(response.status)) {
      try {
        const originalScene = JSON.parse(response.data.originalScene) as {
          shareKey?: string;
          agentId?: string;
        };
        if (originalScene.agentId) {
          console.log(originalScene,111)
          fetchAgentProfile(originalScene.agentId);
          postLog(originalScene.agentId, originalScene.shareKey ?? "");
        }
      } catch (e) {
        Taro.showToast({
          title: "没有对应的知能体",
          icon: "error",
        });
      }
    }
  };
  console.log(params.scene, 1111);
  // 通过小程序码识别打开的页面
  useEffect(()=> {
    const scene = decodeURIComponent(params.scene ?? "");
    if (scene.length <= 32 && scene.length > 0) {
      // 有值，且小于 32 位，说明需要解码
      // 判断是否需要解压 shareKey
      decodeSceneValue(scene);
    }
  }, [])

  // 正常分享出来的页面
  useEffect(() => {
    if (params.agentId) {
      fetchAgentProfile(params.agentId);
      postLog(params.agentId, params.shareKey);
    }
  }, [params.agentId]);

  const renderNavBarLeft = () => {
    const pages = getCurrentPages();
    return (
      <View className={style.navButtons}>
        {pages.length > 1 && (
          <>
            <View
              className="flex-center"
              onClick={() => {
                Taro.navigateBack();
              }}
            >
              <IconArrowLeft></IconArrowLeft>
            </View>
            <View className={style.navGapLine}></View>
          </>
        )}
        <View
          className="flex-center"
          onClick={() => {
            Taro.switchTab({
              url: "/pages/index/index",
            });
          }}
        >
          <IconHomeOutline></IconHomeOutline>
        </View>
      </View>
    );
  };
  
  return (
    <PageCustom styleBg={agentProfile?.avatarUrl}>
      <View className="w-full">
        <NavBarNormal
          leftColumn={renderNavBarLeft}
        ></NavBarNormal>
        <View className="blur-rounded-container">
          {agentProfile && (
            <>
              <SummaryBar isVisitor agent={agentProfile}></SummaryBar>
              <View className="flex flex-col w-full p-16 gap-12">
                <ComponentList
                  components={agentProfile.components ?? []}
                ></ComponentList>
              </View>
            </>
          )}
        </View>
        
      </View>
    </PageCustom>
  );
}
