import APP from '@/services';
import { getLoginData } from '@/services/bus/login';
import { QueryQuoteDayRsp } from '@/services/go/quote/interface';
import { LongType } from '@/services/socket/login/interface/index';
import { Ref } from 'vue';
import { AccountListItem } from '../dataCenter/interafce/account';

export function getAllTaAccount(): AccountListItem[] {
    return APP.get('accountList')
}

/**
 * 获取资金账号列表
 * @return string 拼接之后的字符串
 */
export const getAccoutIdList = (): string => {
    const accountList = getAllTaAccount();
    return accountList.reduce((prev: string, cur: any, i: number) => {
        const id = String(cur.AccountId);
        const temp = i ? `,${id}` : id;
        prev += temp;
        return prev;
    }, '');
};

// taaccounttype: number;//账号类型 - 1:外部账号 2:内部账号 3:内部做市自营账号 4:内部做市接单账号
type taaccounttype = 1 | 2 | 3 | 4
export function getAccountTypeList(type: taaccounttype[]): AccountListItem[] {
    return APP.get('accountList').filter((e: AccountListItem) => type.includes(e.taaccounttype as taaccounttype))
}

// 获取内部资金账号
// 先判断当前选中的资金账号是否为内部账号，是的话取当前的账号，不是的话，取内部账号列表里的第一个
export function getInTaAccount(): number {
    const seleced = getSelectedAccount()
    let result: number = seleced.accountid
    if (seleced.taaccounttype !== 2) {
        const list = getAccountTypeList([2])
        if (list.length) {
            result = list[0].accountid
        } else {
            throw new Error('未获取到内部资金账号，请确认！')
        } ''
    }
    return result
}

/**
 * 获取第一个资金账号
 * @rerurn long型 | null
 */
export const getAccount_longType = (): LongType | null => {
    const accountList = APP.get('accountList');
    if (accountList.length > 0) {
        return accountList[0].accountid;
    }
    return null;
};

/**
 *  获取用户id
 */
export const getUserId = (): number => {
    const loginData = getLoginData();
    if (loginData) {
        return loginData.UserID;
    } else {
        console.warn('没有获取到用户id');
        return 0;
    }
};


export function getQuoteDayInfo(): Ref<QueryQuoteDayRsp[]> {
    return APP.getRef('quoteDayInfo');
}
/**
 * 计算浮动盈亏
 */
function calcHolderSumDynProfit() {
    // const holds = APP.get('quoteDayInfo');
    // const HsbyListingGoods = APP.get('HsbyListingGoods');
    // for (let i = 0; i < holds.length; i++) {
    //     const itemHold = holds[i];
    //     const goodsInfo = HsbyListingGoods.find((e: any) => e.goodsid === itemHold.goodsid);
    //     if (goodsInfo) {
    //     }
    // }
}


/**
 * 获取可用余额
 */
export function getCanUserMoney() {
    const list = APP.get('accountList');
    let result = 0;
    if (list && list.length) {
        const { CurrentBalance, FreezeMargin, UsedMargin, OtherFreezeMargin, FreezeCharge } = list[0];
        result = CurrentBalance - FreezeMargin - UsedMargin - OtherFreezeMargin - FreezeCharge;
        result = +result.toFixed(2);
    } else {
        console.error('获取资金列表错误');
    }
    return result;
}

// 获取选中的资金账号信息
export function getSelectedAccount(): AccountListItem {
    return APP.get('selectedAccount')
}

// 设置当前中的资金账号
export function setSelectedAccount(value: AccountListItem) {
    APP.set('selectedAccount', value)
}

/**
 * 获取选中的资金账号
 */
export function getSelectedAccountId(): number {
    return getSelectedAccount().accountid
}

// 获取冻结资金
export function getFreeze(value: AccountListItem, isFixed2 = false) {
    const { freezecharge, freezemargin, otherfreezemargin, outamountfreeze } = value;
    const freeze = freezecharge + freezemargin + otherfreezemargin + outamountfreeze;
    return isFixed2 ? freeze.toFixed(2) : freeze
}

// 获取可用资金
export function getCanUseMoney(value: AccountListItem) {
    const freeze = getFreeze(value) as number
    return (value.currentbalance - freeze).toFixed(2)
}