import {ApiCall} from "tsrpc";
import {EquipFun} from '../../public/equip';
import {PlayerFun} from '../../public/player';
import {ReqReset, ResReset} from "../../shared/protocols/equip/PtlReset";
import {prizeType} from '../../shared/protocols/type';
import {EquipShared} from '../../shared/public/equip';
import {PublicShared} from '../../shared/public/public';

export default async function (call: ApiCall<ReqReset, ResReset>) {
    let req = call.req;
    let equips = await EquipFun.getEquip(call, req._ids);

    if (equips.length != req._ids.length) return call.errorCode(-1);
    if (equips.some(e => e.wearaId != '')) return call.errorCode(-2);
    if (req.type == 'cj' && equips.some(e => (e.lv || e.star || e.adjustment) && G.gc.equip[e.equipId].colour > 3)) return call.errorCode(-3);
    if (req.type == 'yl') {
        let ylprize: prizeType[] = [];
        for (let equip of equips) {
            if (equip.lv) {
                ylprize.push(...new Array(equip.lv).fill(1).map((v, i) => EquipShared.getEquipLvUpNeed({lv: i + 1})).reduce((a, b) => a.concat(b)));
            }
            if (equip.star) {
                ylprize.push(...new Array(equip.star).fill(1).map((v, i) => G.gc.equipstar[equip.equipId][i + 1].need).reduce((a, b) => a.concat(b)));
            }
            if (equip.adjustment) {
                ylprize.push(...new Array(equip.adjustment).fill(1).map((v, i) => G.gc.equipAdjusting[i + 1].need).reduce((a, b) => a.concat(b)));
            }
        }
        ylprize = PublicShared.mergePrize(ylprize);
        ylprize.forEach(p => {
            if (p.t == 'jinbi') p.n = Math.floor(p.n * G.gc.guanlidiaodu.equip_cz_jinbi);
        });
        return call.succ({
            prize: ylprize
        });
    }
    let need = G.gc.guanlidiaodu[req.type == 'cz' ? 'equip_cz_need' : 'equip_qs_need'].map(n => {
        return {
            ...n,
            n: n.n * equips.length
        };
    });
    await PlayerFun.checkNeedIsMeet(call, need);
    await PlayerFun.cutNeed(call, need);

    let prize: prizeType[] = [];


    for (let equip of equips) {

        if (equip.lv) {
            prize.push(...new Array(equip.lv).fill(1).map((v, i) => EquipShared.getEquipLvUpNeed({lv: i + 1})).reduce((a, b) => a.concat(b)));
        }

        if (equip.star) {
            prize.push(...new Array(equip.star).fill(1).map((v, i) => G.gc.equipstar[equip.equipId][i + 1].need).reduce((a, b) => a.concat(b)));
        }

        if (equip.adjustment) {
            prize.push(...new Array(equip.adjustment).fill(1).map((v, i) => G.gc.equipAdjusting[i + 1].need).reduce((a, b) => a.concat(b)));
        }

        if (req.type == 'cj') continue

        await EquipFun.changeEquipAttr(call, equip, {lv: 0, star: 0, adjustment: 0});
    }
    if (req.type == 'cj') {
        await PlayerFun.cutEquip(call, req._ids);
        prize.push(...equips.map(c => G.gc.guanlidiaodu.equip_qs_prize[G.gc.equip[c.equipId].colour] as any[]).reduce((a, b) => a.concat(b)));
    }

    prize = PublicShared.mergePrize(prize);
    prize.forEach(p => {
        if (p.t == 'jinbi') p.n = Math.floor(p.n * G.gc.guanlidiaodu.equip_cz_jinbi);
    });

    await PlayerFun.sendPrize(call, prize);

    call.succ({
        prize: prize
    });
}