
import {
  useGetDesktopCloudListTree,
  usePostDesktopCloudLockModule,
  usePostDesktopCloudUnlockModule,
  useGetDesktopModuleInfo,
  // useGetDesktopCloudUserLockModules
} from '@/generated/api';
import {useSystemContext} from '@/system/system.context';
import {MenuItem} from '../layout.interface';
import {ModuleItem} from './module.interface';

interface ChangePasswordProps {
  oldPasswd: string;
  newPasswd: string;
}

export const useModuleList = () => {
  const {store, history = {}} = useSystemContext();
  const [_, dispatch] = store();
  const { getDesktopCloudListTreeData, isDesktopCloudListTreeLoading} = useGetDesktopCloudListTree();
  const {postDesktopCloudLockModuleData} = usePostDesktopCloudLockModule();
  const {postDesktopCloudUnlockModuleData} = usePostDesktopCloudUnlockModule();
  // const {getDesktopCloudUserLockModulesData} = useGetDesktopCloudUserLockModules();
  const {getDesktopModuleInfoData} = useGetDesktopModuleInfo();

  const initDesktopMenu = async (menus: MenuItem[], moduleName = '', moduleId?: string) => {
    console.log('initDesktopMenu', moduleId)
    // const res = await getDesktopCloudUserLockModulesData();
    const result: ModuleItem[] = menus;
    // const desktopMenus = res.list;
    let currentMenuName = moduleName;
    currentMenuName = currentMenuName.replace('/', '');
    let path = `/${currentMenuName}`;
    let currentData: ModuleItem = {
      name: currentMenuName,
      path,
      meta: {
        title: currentMenuName,
      },
    }
    let currentMenu: ModuleItem = {
      ...currentData,
      onClick: async () => {
        history && history.push(path);
      },
      onClose: async () => {

        const res = await closeModule(currentData);
        history && history.push(res.path);
      }
    };
    if(moduleId && moduleName) {
      const module = await getDesktopModuleInfoData({moduleId});
      if(module && module.moduleName){
        path = `/${moduleName}/${moduleId}`;
        currentData = {
          name: moduleId,
          path,
          meta: {
            title: module.moduleName,
            icon: `${module.moduleIcon}-xiao`,
          },
        }
        currentMenu = {
          ...currentData,
          onClick: async () => {
            history && history.push(path);
          },
          onClose: async () => {
            const res = await closeModule(currentData);
            history && history.push(res.path);
          }
        };
        result.push(currentMenu)
      }
    }
    
    // if(desktopMenus && desktopMenus.length){
    //   desktopMenus.map((data: any) => {
    //     const menu = transformToDesktopMenus(data);
    //     if(menu.name === currentMenuName){
    //       currentMenu = menu;
    //     }
    //     menu.isLock = true;
    //     result.push(menu);
    //   });
    // } else {
    //   currentMenu = {
    //     name: currentMenuName,
    //     path: `/${currentMenuName}`,
    //     meta: {
    //       title: 'currentMenuName',
    //     },
    //   }
    // }

    dispatch({
      type: 'init_menus',
      payload: {
        data: {
          desktopMenus: result,
          currentMenu
        }
      }
    })
    return result;
  }

  const initModuleList = async () => {
    const res = await getDesktopCloudListTreeData();
    if(res && res.list) {
      dispatch({
        type: 'init_module_list',
        payload: {
          data: res.list
        }
      });
    }
    return res.list || [];
  }

  const selectModule = async (data: any) => {
    dispatch({
      type: 'select_module',
      payload: {
        data
      }
    })
  }

  const lockModule = async (data: any) => {
    const {moduleId} = data;
    const res = await postDesktopCloudLockModuleData({moduleId});
    if(res){
      dispatch({
        type: 'lock_module',
        payload: {
          data: data
        }
      });
    }
    return res;
  }

  const unLockModule = async (data: any) => {
    const {moduleId} = data;
    const res = await postDesktopCloudUnlockModuleData({moduleId});
    if(res){
      dispatch({
        type: 'unlock_module',
        payload: {
          data
        }
      });
    }
    return res;
  }

  const closeModule = async (data: any) => {
    const res = await dispatch({
      type: 'close_module',
      payload: {
        data: data.name ? data : transformToDesktopMenus(data)
      }
    });
    return res.currentMenu
  }

  const updateMenu = (data: any) => {
    dispatch({
      type: 'update_menu',
      payload: {
        data
      }
    })
  }

  const transformToDesktopMenus = (data: any) => {
    const path =  `/${data.moduleNameEn}/${data.moduleId}`;
    return {
      moduleId: data.moduleId,
      path,
      name: data.moduleId,
      meta: {
        title: data.moduleName,
        icon: `${data.moduleIcon}-xiao`
      },
      isLock: data.isLock,
      onClick: async () => {
        history && history.push(path);
      },
      onClose: async () => {
        const res = await closeModule(data);
        history && history.push(res.path);
      }
    }
  }

  const linkModule = (menu: ModuleItem) => {
    console.log('linkModule', menu)
    history && history.push(menu.path);
  }

  return {
    updateMenu,
    linkModule,
    initDesktopMenu,
    initModuleList,
    selectModule,
    lockModule,
    transformToDesktopMenus,
    isDesktopCloudListTreeLoading
  };
};
