import { defineStore } from 'pinia';
import { store } from '@/store';

export const usePrepareStore = defineStore('prepare-lessons-store', {
  state: () => ({
    subjectId: null as any, // 基础声部
    instrumentId: null as any, // 基础乐器
    baseCourseware: {} as any, // 基础教学课件
    selectKey: '', // 选的哪一节课
    lessonCoursewareId: '', // 哪个教材分类
    subjectList: [] as any, // 教材带的声部列表
    instrumentList: [] as any, // 教材自带的乐器
    lessonCoursewareDetailId: '', // 哪个教材详情
    treeList: [] as any[], // 左边教学课件列表
    coursewareList: [] as any[], // 课件信息
    trainList: [] as any[], // 训练信息
    tabType: 'courseware', // 备课 - 课件 | 训练 类型切换 'courseware' | 'train'
    selectMusicStatus: false, // 乐谱状态
    selectResourceStatus: false, // 资源状态
    isAddResource: false, // 是否添加资源
    isEditResource: false, // 是否编辑资源 - 课件编辑状态
    iseditTrain: false, // 是否编辑训练,
    isAddTrain: false, // 是否添加训练,
    classGroupId: null as any // 班级编号
  }),
  getters: {
    /** 获取资源状态 */
    getSubjectId(): [string, number] {
      return this.subjectId;
    },
    /** 获取乐器编号 */
    getInstrumentId(): [string, number] {
      return this.instrumentId;
    },
    /** 获取基础教学课件 */
    getBaseCourseware(): any {
      return this.baseCourseware;
    },
    /** 获取选择课的编号 */
    getSelectKey(): string {
      return this.selectKey;
    },
    /** 获取选择课的名称 */
    getSelectName(): string {
      let title = '';
      for (let index = 0; index < this.getTreeList.length; index++) {
        const element = this.getTreeList[index];
        if (element.id === this.selectKey) {
          title = element.name;
        }

        if (!title && element.knowledgeList.length > 0) {
          element.knowledgeList?.forEach((item: any) => {
            if (item.id === this.selectKey) {
              title = item.name;
            }
          });
        }
      }
      return title;
    },
    /** 获取教材编号 */
    getLessonCoursewareId(): string {
      return this.lessonCoursewareId;
    },
    /** 获取分类编号 */
    getLessonCoursewareDetailId(): string {
      return this.lessonCoursewareDetailId;
    },
    /** 获取树形控件 */
    getTreeList(): any[] {
      return this.treeList;
    },
    /** 获取课件列表 */
    getCoursewareList(): any[] {
      return this.coursewareList;
    },
    /** 获取训练列表 */
    getTrainList(): any[] {
      return this.trainList;
    },
    /** 获取课件类型 */
    getTabType(): string {
      return this.tabType;
    },
    /** 获取乐谱状态 */
    getSelectMusicStatus(): boolean {
      return this.selectMusicStatus;
    },
    /** 获取资源状态 */
    getSelectResourceStatus(): boolean {
      return this.selectResourceStatus;
    },
    /** 获取是否添加资源 */
    getIsAddResource(): boolean {
      return this.isAddResource;
    },
    /** 获取是否修改资源 */
    getIsEditResource(): boolean {
      return this.isEditResource;
    },
    /** 获取是否修改训练 */
    getIsEditTrain(): boolean {
      return this.iseditTrain;
    },
    /** 获取是否添加训练 */
    getIsAddTrain(): boolean {
      return this.isAddTrain;
    },
    /** 获取声部列表 */
    getSubjectList(): any {
      return this.subjectList;
    },
    /** 获取乐器列表 */
    getInstrumentList(): any {
      return this.instrumentList;
    },
    /** 获取乐器格式化列表 */
    getFormatInstrumentList(): any {
      const tempSubjectInstruments: any = [];
      this.instrumentList.forEach((item: any) => {
        item.value = item.id;
        item.label = item.name;
        if (item.instruments && item.instruments.length > 0) {
          item.instruments.forEach((child: any) => {
            child.label = child.name;
            child.value = child.id;
          });
        }

        const tempSi = {
          value: item.id,
          label: item.name,
          id: item.id,
          name: item.name,
          instruments: [] as any
        };
        if (item.instruments) {
          if (item.instruments.length == 1) {
            tempSi.value = item.instruments[0].id;
            tempSi.label = item.instruments[0].name;
            tempSi.id = item.id;
            tempSi.name = item.name;
          } else if (item.instruments.length > 1) {
            item.instruments.forEach((child: any) => {
              child.label = child.name;
              child.value = child.id;
              tempSi.instruments.push({
                label: child.name,
                value: child.id,
                id: child.id,
                name: child.name
              });
            });
          }
        }
        tempSubjectInstruments.push(tempSi);
      });
      return tempSubjectInstruments;
    },
    /** 获取所有乐器列表 */
    getSingleInstrumentList(): any {
      const temps: any = [];
      this.instrumentList.forEach((item: any) => {
        if (Array.isArray(item.instruments)) {
          item.instruments.forEach((child: any) => {
            temps.push(child);
          });
        }
      });
      return temps;
    },
    /** 获取班级编号 */
    getClassGroupId(): string | number {
      return this.classGroupId;
    }
  },
  actions: {
    /** 设置基础声部 */
    setSubjectId(subjectId: string | number) {
      this.subjectId = subjectId;
    },
    /** 获取乐器编号 */
    setInstrumentId(instrumentId: string | number) {
      this.instrumentId = instrumentId;
    },
    /** 设置基础教学课件 */
    setBaseCourseware(baseCourseware: any) {
      this.baseCourseware = baseCourseware;
    },
    /** 设置课的编号 */
    setSelectKey(key: string) {
      this.selectKey = key;
    },
    /** 设置教材的编号 */
    setLessonCoursewareId(id: string) {
      this.lessonCoursewareId = id;
    },
    /** 设置分类的编号 */
    setLessonCoursewareDetailId(id: string) {
      this.lessonCoursewareDetailId = id;
    },
    /** 设置课的编号 */
    setTreeList(list: any[]) {
      this.treeList = list;
    },
    /** 设置课件列表 */
    setCoursewareList(list: any[]) {
      this.coursewareList = list;
    },
    /** 设置训练列表 */
    setTrainList(list: any[]) {
      this.trainList = list;
    },
    /** 设置tab类型 */
    setTabType(type: string) {
      this.tabType = type;
    },
    /** 设置乐谱状态 */
    setSelectMusicStatus(status: boolean) {
      this.selectMusicStatus = status;
    },
    /** 设置资源状态 */
    setSelectResourceStatus(status: boolean) {
      this.selectResourceStatus = status;
    },
    /** 设置资源状态 */
    setIsAddResource(status: boolean) {
      this.isAddResource = status;
    },
    /** 设置训练状态 */
    setIsAddTrain(status: boolean) {
      this.isAddTrain = status;
    },
    /** 设置资源状态 */
    setIsEditResource(status: boolean) {
      this.isEditResource = status;
    },
    /** 设置训练状态 */
    setIsEditTrain(status: boolean) {
      this.iseditTrain = status;
    },
    /** 设置声部列表 */
    setSubjectList(subjects: any): any {
      this.subjectList = subjects;
    },
    /** 设置乐器列表 */
    setInstrumentList(instruments: any): any {
      this.instrumentList = instruments;
    },
    /** 设置班级编号 */
    setClassGroupId(id: string | number): any {
      this.classGroupId = id;
    }
  }
});

// Need to be used outside the setup
export function usePrepareLessonsStoreWidthOut() {
  return usePrepareStore(store);
}
