import WemetaTabs from "@/components/wemeta-tabs/index";
import VoicePlayerBar, { IVoicePlayerBar } from "@/components/voice-player-bar";
import ScrollVoiceList from "./components/ScrollVoiceList";
import MyVoiceList from "./components/MyVoiceList/index";
import { View } from "@tarojs/components";
import React, { useEffect, useRef, useState } from "react";
import NavBarNormal from "@/components/NavBarNormal/index";
import style from "./index.module.less";
import PageCustom from "@/components/page-custom/index";
import { useVoiceStore } from "@/store/voiceStore";
import { TVoiceItem } from "@/types/voice";
import { useAgentStore, useAgentStoreActions } from "@/store/agentStore";


interface Props {}

const VoiceTabs: React.FC<Props> = ({}) => {
  const playerRef = useRef<IVoicePlayerBar>(null);
  const [sysVoice, setSysVoice] = useState<TVoiceItem | null>(
    null
  );

  const agentEdit = useAgentStore((state) => state.agentEdit);
  const { saveAgent, updateEditAgent } = useAgentStoreActions();

  const {
    getVoices,
  } = useVoiceStore();



  // 获取女声列表
  useEffect(() => {
    getVoices()
  }, [agentEdit]);

  // 保存默认 voiceId
  const saveAgentVoiceId = async (voiceItem: TVoiceItem) => {
    if(!voiceItem?.voiceId){
      return
    }
    if(!agentEdit?.agentId){
      updateEditAgent({voiceId: voiceItem.voiceId, voiceName: voiceItem.voiceName})
      return
    }
    const result = await saveAgent({
      voiceId: voiceItem.voiceId,
      voiceName: voiceItem.voiceName
    })
    console.log(result)
  }

  // 设置当前播放器的播放音频信息
  const setPlayerItem = async (voiceItem: TVoiceItem, type: "system" | "cloned") => {
    setSysVoice(voiceItem);
    saveAgentVoiceId(voiceItem)
  };
  const onPlay = async (voiceItem: TVoiceItem, type: "system" | "cloned") => {
    if(playerRef.current){
      setSysVoice(voiceItem)
      playerRef.current.stop();
      playerRef.current.play(voiceItem.voiceUrl)
    }
  };



  const tabList = [
    {
      label: "我的",
      key: "1",
      children: (
        <View className="h-full">
          <MyVoiceList
          agent={agentEdit}
          onSelect={(item) => {
            setPlayerItem(item, "cloned");
          }}
          onPlay={(item) => {
            onPlay(item, "cloned");
          }}
          ></MyVoiceList>
        </View>
      ),
    },
    {
      label: "全部",
      key: "2",
      children: (
        <View className="h-full bg-white">
          <ScrollVoiceList
            agent={agentEdit}
            key="ScrollVoiceList"
            onSelect={(item) => {
              setPlayerItem(item, "system");
            }}
            onPlay={(item) => {
              onPlay(item, "system");
            }}
          >

          </ScrollVoiceList>
        </View>
      ),
    },
    {
      label: "女声",
      key: "3",
      children: (
        <View className="h-full bg-white">
          <ScrollVoiceList
            agent={agentEdit}
            key="femaleVoice"
            gender="female"
            onSelect={(item) => {
              setPlayerItem(item, "system");
            }}
            onPlay={(item) => {
              onPlay(item, "system");
            }}
          >

          </ScrollVoiceList>
        </View>
      ),
    },
    {
      label: "男声",
      key: "4",
      children: (
        <View className="h-full bg-white">
          <ScrollVoiceList
            agent={agentEdit}
            key="maleVoice"
            gender="male"
            onSelect={(item) => {
              setPlayerItem(item, "system");
            }}
            onPlay={(item) => {
              onPlay(item, "system");
            }}
          >

          </ScrollVoiceList>
        </View>
      ),
    },
  ];



  return (
    <PageCustom fullPage >
      <NavBarNormal backText="声音"></NavBarNormal>
      <View className={style.container}>
        <View className={style.playContainer}>
          <VoicePlayerBar
            ref={playerRef}
            voiceItem={sysVoice}
          />
        </View>
        <View className={style.voiceTab}>
          <WemetaTabs
            list={tabList}
            current="1"
            className="bg-white"
            tabStyle="outline"
          ></WemetaTabs>
        </View>
      </View>
    </PageCustom>
  );
};

export default VoiceTabs;
