import { ITID, CONSUME_TYPE, ITEM_TABLE, CURRENCY, CURRENCY_TYPE, HANDLE_REWARD_TYPE, ITEM_CHANGE_REASON, CURRENCY_BY_TYPE } from '../../consts';
import { ItemInter, RewardInter, } from '../../pubUtils/interface';
import { gameData } from '../../pubUtils/data';
import { errlogger } from '../../util/logger';

export function sortItems(goods: ItemInter[], handleType: HANDLE_REWARD_TYPE) {
    let items: { id: number, count: number }[] = [];  // 可叠加道具
    let jewels: { seqId?: number, id?: number, hid?: number }[] = []; // 不可叠加装备
    let artifacts: { seqId?: number, id?: number }[] = []; // 不可叠加宝物
    let gold: { count: number, isPay: boolean }[] = []; // 金币
    let coin: number[] = [];
    let ap: number = 0;
    let skins: number[] = [];
    let figures: number[] = [];

    for(let good of goods) {
        if(good.count == 0) continue;
        let dicGood = gameData.goods.get(good.id);
        if(!dicGood) {
            errlogger.error(`物品 ${good.id} 未配置`);
            continue;
        }
        let dicItid = ITID.get(dicGood.itid);
        if(!dicItid) {
            errlogger.error(`itid ${dicGood.itid} 未配置`);
            continue;
        }
        let { type, table, isCurrency } = dicItid;
        if(table == ITEM_TABLE.JEWEL) { // 装备
            if(handleType == HANDLE_REWARD_TYPE.RECEIVE) {
                for(let i = 0; i < good.count; i++) {
                    jewels.push({ id: good.id, hid: good.hid })
                }
            } else {
                if(!!good.seqId) {
                    jewels.push({ seqId: good.seqId });
                }
            }
        } else if (table == ITEM_TABLE.ITEM) { // 可叠加道具
            let index = items.findIndex(cur => cur.id == good.id);
            if(index > 0) {
                items[index].count += good.count;
            } else {
                items.push({ id: good.id, count: good.count });
            }
        } else if (table == ITEM_TABLE.SKIN) { // 皮肤，不可重复获得，不可删
            if(handleType == HANDLE_REWARD_TYPE.RECEIVE) {
                let index = skins.indexOf(good.id);
                if (index == -1) {
                    skins.push(good.id);
                }
            }
        } else if (table == ITEM_TABLE.ROLE) {
            if(isCurrency) { // 3种货币
                let dicCurrency = CURRENCY.get(good.id);
                if(dicCurrency) {
                    if(dicCurrency.type == CURRENCY_TYPE.GOLD) { // 金币，区分付费和免费，默认免费
                        let index = gold.findIndex(cur => cur.isPay == !!good.isPay);
                        if(index > 0) {
                            gold[index].count += good.count;
                        } else {
                            gold.push({ count: good.count, isPay: !!good.isPay });
                        }
                    } else if (dicCurrency.type == CURRENCY_TYPE.COIN) { // 铜钱
                        coin.push(good.count);
                    } else if (dicCurrency.type == CURRENCY_TYPE.ACTION_POINT) { // 体力
                        ap += good.count;
                    }
                }
            } else { 
                if (type == CONSUME_TYPE.HEAD || type == CONSUME_TYPE.FRAME || type == CONSUME_TYPE.SPINE) { // 头像等，不可重复获得，不可删
                    let index = figures.indexOf(good.id);
                    if (index == -1) {
                        figures.push(good.id);
                    }
                }
            }
        } else if (table == ITEM_TABLE.ARTIFACT) {
            if(handleType == HANDLE_REWARD_TYPE.RECEIVE) {
                for(let i = 0; i < good.count; i++) {
                    artifacts.push({ id: good.id })
                }
            } else {
                if(!!good.seqId) {
                    artifacts.push({ seqId: good.seqId });
                }
            }
        }

    }

    return { items, jewels, gold, coin, ap, skins, figures, artifacts }
}

export function getGoldEventProperties(inc: number, count: number, reason: ITEM_CHANGE_REASON) {
    let id = CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD);
    let dicGoods = gameData.goods.get(id);
    return { item_id: id, item_name: dicGoods.name, item_itid: dicGoods.itid, change_count: inc, change_after: count, change_reason: reason }
}

export function getCoinEventProperties(inc: number, count: number, reason: ITEM_CHANGE_REASON) {
    let id = CURRENCY_BY_TYPE.get(CURRENCY_TYPE.COIN);
    let dicGoods = gameData.goods.get(id);
    return { item_id: id, item_name: dicGoods.name, item_itid: dicGoods.itid, change_count: inc, change_after: count, change_reason: reason }
}

export function combineItems(items: { id?: number, count?: number, seqId?: number, isBag?: boolean }[]) {
    let result: { id: number, count: number, seqId?: number, isBag?: boolean }[] = [];
    for(let { id, count = 1, seqId, isBag } of items) {
        let dicGoods = gameData.goods.get(id);
        let dicItid = ITID.get(dicGoods.itid);
        if(dicItid.table != 'jewel' && dicItid.table != 'artifact') {
            let index = result.findIndex(cur => cur.id == id);
            if(index == -1) {
                result.push({ id, count, seqId, isBag });
            } else {
                result[index].count += count;
            }
        } else {
            result.push({ id, count, seqId, isBag });
        }
    }
    return result;
}


export function transPiece(hid: number) {
    let dicHero = gameData.hero.get(hid);
    let count = gameData.heroTransPiece.get(dicHero.quality);
    return { pieceId: dicHero.pieceId, count }
}