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

export default async function (call: ApiCall<ReqReset, ResReset>) {
    let req = call.req;
    if (req.type == 'qs' && Object.values(call.conn.gud.heroPos).filter(_id => _id != '').concat(call.conn.gud.helpHeros.map(h => h._id)).intersection(req._ids).length > 0) return call.errorCode(-1);

    let heros = (await HeroFun.getHeros(call, req._ids)).filter(h => !!h);
    if (heros.length < req._ids.length) return call.errorCode(-2);

    let prize: prizeType[] = [];
    if (req.type == 'yl') {
        for (let hero of heros) {
            if (hero.lv <= 1 && hero.jieji == 0) {
                let item = G.gc.item[hero.heroId];
                if (item) {
                    prize.push({ a: 'item', t: item.id, n: item.num });
                }
            }
            let p = await HeroFun.delHero(call, hero);
            prize.push(...p);
        }
        prize = PublicShared.mergePrize(prize);
        prize.forEach(p => {
            if (p.t == 'jinbi') p.n = Math.floor(p.n * G.gc.guanlidiaodu.hero_cz_jinbi);
        });
        return call.succ({
            prize: prize
        });
    } else if (req.type == 'cz') {
        // if (heros.filter(h => h.lv <= 1).length > 0) return call.errorCode(-10);

        let need = G.gc.guanlidiaodu.hero_cz_need.map(n => {
            return {
                ...n,
                n: n.n * heros.length
            };
        });
        await PlayerFun.checkNeedIsMeet(call, need);
        await PlayerFun.cutNeed(call, need);

        let oneHeros = heros.filter(v => v.lv <= 1 && v.jieji == 0 ).map(v => v._id);

        for (let hero of heros) {

            if (hero.lv > 1 || hero.jieji > 0) {
                let p = await HeroFun.delHero(call, hero);
                let { _id, uid, ...ops } = hero;
                await HeroFun.changeHeroAttr(call, hero, ops);
                prize.push(...p);
            } else {
                let item = G.gc.item[hero.heroId];
                if (item) {
                    prize.push({ a: 'item', t: item.id, n: item.num });
                }
            }
        }

        oneHeros.length > 0 && await PlayerFun.cutHero(call, oneHeros);
    } else if (req.type == 'qs') {
        if (heros.filter(h => h.lv > 1 || h.jieji > 0).length > 0) return call.errorCode(-20);

        let need = G.gc.guanlidiaodu.hero_qs_need.map(n => {
            return {
                ...n,
                n: n.n * heros.length
            };
        });
        await PlayerFun.checkNeedIsMeet(call, need);
        await PlayerFun.cutNeed(call, need);

        await PlayerFun.cutHero(call, heros.map(h => h._id));
        prize.push(...heros.map(c => G.gc.guanlidiaodu.hero_qs_prize[G.gc.hero[c.heroId].star] as any[]).reduce((a, b) => a.concat(b)));
    }

    prize.forEach(p => {
        if (p.t == 'jinbi') p.n = Math.floor(p.n * G.gc.guanlidiaodu.hero_cz_jinbi);
    });
    prize = PublicShared.mergePrize(prize);
    await PlayerFun.sendPrize(call, prize);

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