import { create } from "zustand";

import { TGetMyVoicesParams, TVoiceItem, TPageination, TGender } from '@/types/voice'

import {
  getVoices as _getVoices,
  cloneVoice as _cloneVoice,
  
} from "@/service/voice";
import { isSuccess } from "@/utils";


export interface StorageStoreState {
  // 初始状态
  voices: TVoiceItem[];
  myVoices: TVoiceItem[];
  maleVoices: TVoiceItem[];
  femaleVoices: TVoiceItem[];
  pagination: TPageination;
  malePagination: TPageination;
  femalePagination: TPageination;
  isLoading: boolean,
  getVoices: (params?: TGetMyVoicesParams) => Promise<boolean>;
  getMaleVoices: (params?: TGetMyVoicesParams) => Promise<boolean>;
  getFemaleVoices: (params?: TGetMyVoicesParams) => Promise<boolean>;
  setPagination: (params:any) => Promise<void>;
  setMalePagination: (params:any) => Promise<void>;
  setFemalePagination: (params:any) => Promise<void>;
  cloneVoice: (params: {
    sourceUrl: string // 源语音地址 ,
    voiceText?: string // 录音文案
  }) => Promise<void>;
}

export const useVoiceStore = create<StorageStoreState>((set, get) => ({
  voices: [],
  myVoices: [],
  maleVoices: [],
  femaleVoices: [],
  pagination: {
    pageIndex: 1,
    pageSize: 10,
    totalCount: 0,
    searchKey: '',
    extData: null,
  },
  malePagination: {
    pageIndex: 1,
    pageSize: 10,
    totalCount: 0,
    searchKey: '',
    extData: null,
  },
  femalePagination: {
    pageIndex: 1,
    pageSize: 10,
    totalCount: 0,
    searchKey: '',
    extData: null,
  },
  isLoading: false,
  getVoices: async (params = {}) => {
    set({ isLoading: true });
    // 合并当前分页参数和传入参数
    const currentPagination = get().pagination;
    const requestParams = { ...currentPagination, ...params };

    const response = await _getVoices(requestParams)

    // 更新状态
    set({
      voices: response.data.data,
      myVoices: response.data.data.filter(item => !item.isSystem).reverse(),
      pagination: {
        ...requestParams,
        totalCount: response.data.totalCount,
      },
      isLoading: false
    });
    return isSuccess(response.status)
  },
  getMaleVoices: async (params = {}) => {
    set({ isLoading: true });
    const currentPagination = get().malePagination;
    const requestParams = { ...currentPagination, ...params, gender: 'male' as TGender };

    const response = await _getVoices(requestParams)

    set({
      maleVoices: response.data.data,
      malePagination: {
        ...requestParams,
        totalCount: response.data.totalCount,
      },
      isLoading: false
    });
    return isSuccess(response.status)
  },
  getFemaleVoices: async (params = {}) => {
    set({ isLoading: true });
    const currentPagination = get().femalePagination;
    const requestParams = { ...currentPagination, ...params, gender: 'female' as TGender };

    const response = await _getVoices(requestParams)

    set({
      femaleVoices: response.data.data,
      femalePagination: {
        ...requestParams,
        totalCount: response.data.totalCount,
      },
      isLoading: false
    });
    return isSuccess(response.status)
  },
  // 更新分页参数
  setPagination: async (params:any) => {
    set((state) => ({
      pagination: { ...state.pagination, ...params },
    }));
    // 可选：自动触发请求
    get().getVoices();
  },
  setMalePagination: async (params:any) => {
    set((state) => ({
      malePagination: { ...state.malePagination, ...params },
    }));
    get().getMaleVoices();
  },
  setFemalePagination: async (params:any) => {
    set((state) => ({
      femalePagination: { ...state.femalePagination, ...params },
    }));
    get().getFemaleVoices();
  },
  cloneVoice: async (params) => {
    const response = await _cloneVoice(params)
    console.log(response,444444) 
  }
}));
