import { isOemByEnum, OemType } from '@/common/config/projectName';
import { TradeMode } from '@/common/constants/enumCommon';
import APP from '@/services';
import { isAllEnum } from '@/services/bus/allEnum';
import { checkTokenLoop } from '@/services/bus/token';
import { getAllEnums, GetPCMenus, getServerTime, queryErrorInfos, QueryTableDefine } from '@/services/go/commonService/index';
import { GetErmcpGoods, QueryDeliveryGoodsDetail } from '@/services/go/ermcp/goodsInfo';
import { GetLoginID, LoginQuery } from '@/services/go/useInfo/index';
import { sessionStorageUtil } from '@/utils/storage/index';
import Long from 'long';
import { queryMarketRun } from '../go/commonMarket';
import { getTaAccount } from '../go/TaAccount';
import { queryWrMarketTradeConfig } from '../go/wrtrade';
import { logoutResponse } from '../socket/login';
import { loginAction } from '../socket/login/index';
import { LoginResponse, LongType } from '../socket/login/interface/index';
import { getMarketByTradeMode } from './market';

export const logout = (): void => {
    // removeLoginData();
    logoutResponse();
};

export const login = async (logidCode: string, password: string, byteArr: Uint8Array[], isEncryt = false): Promise<LoginResponse> => {
    try {
        /** 建立行情交易长链 */
        await APP.connectTrading();
        const account = await GetLoginID(logidCode);
        const loginData = await loginAction(account, password, byteArr, isEncryt);
        // localstorage 缓存登录信息，处理页面刷新
        setLoginData(loginData);
        await Promise.all([GetErmcpGoods(), GetPCMenus(), QueryTableDefine(), getServerTime(), LoginQuery(), QueryDeliveryGoodsDetail(), getTaAccount(), queryMarketRun()])
        const marketInfo = getMarketByTradeMode(TradeMode.WarehouseReceiptTrade);
        if (marketInfo) {
            if (isOemByEnum(OemType.wrspot)) {
                // 仓单贸易交易规则
                await queryWrMarketTradeConfig({ marketid: marketInfo.marketid })
            }
        }

        // await GetPCMenus();
        // await QueryTableDefine();
        // 获取服务时间
        // await getServerTime();
        // 获取错误信息
        await queryErrorInfos();
        // 获取枚举
        if (!isAllEnum()) {
            await getAllEnums();
        }

        // 校验token
        checkTokenLoop();
        // await LoginQuery();
        return loginData;
    } catch (err) {
        return Promise.reject('登录失败:' + err);
    }
};

/**
 * 缓存登录信息
 */
export function setLoginData(loginData: LoginResponse) {
    console.log('loginData', loginData);

    sessionStorageUtil.setItem('loginData', loginData);
}

/**
 * 删除登录信息
 */
export function removeLoginData(): void {
    sessionStorageUtil.removeItem('loginData');
}

/**
 * 获取登录信息
 * @returns LoginResponse
 */
export function getLoginData(): LoginResponse {
    return sessionStorageUtil.getItem('loginData');
}

/**
 * 是否登录
 */
export const isLogin = (): boolean => {
    const loginData = getLoginData();
    return loginData ? true : false;
};

/**
 * 获取当前登录id的方法
 */
export const getLongTypeLoginID = (): LongType | null => {
    if (isLogin()) {
        const loginData = getLoginData().LoginID;
        const { low, high, unsigned } = loginData;
        return new Long(low, high, unsigned);
    } else {
        return null;
    }
};
