import { ApiCall } from "tsrpc";
import { Wjjl } from '../../module/collection_wjjl';
import { HeroFun } from '../../public/hero';
import { PlayerFun } from '../../public/player';
import { ResGetList } from '../../shared/protocols/hero/PtlGetList';
import { ReqJinJie, ResJinJie } from "../../shared/protocols/hero/PtlJinJie";
import { ServiceType } from '../../shared/protocols/serviceProto';
import { HeroShared } from '../../shared/public/hero';
import { event_cszl_record } from '../event/chuanshuozhilu/ApiOpen';
import { PublicShared } from "../../shared/public/public";

export default async function (call: ApiCall<ReqJinJie, ResJinJie, ServiceType>) {

    let hero = await HeroFun.getHero(call, call.req.id)

    if (!hero) return call.error(globalThis.lng.hero_1);
    if (G.gc.herogrow[G.gc.hero[hero.heroId].jjup] == undefined) return call.error(globalThis.lng.hero_6);
    if (G.gc.herogrow[G.gc.hero[hero.heroId].jjup][hero.jieji + 1] == undefined) return call.error(globalThis.lng.hero_7);
    if (G.gc.herogrow[G.gc.hero[hero.heroId].jjup][hero.jieji + 1].lv > hero.lv) return call.error(globalThis.lng.hero_8);

    let need = HeroShared.getHeroJinJieNeed(hero.heroId, hero.jieji);
    let needItem = need.filter(v => v.a != 'hero');
    let needHero = need.filter(v => v.a == 'hero');

    await PlayerFun.checkNeedIsMeet(call, needItem);

    if (needHero.length > 0) {
        if (!call.req.idArr || call.req.idArr.length < 1) return call.error(globalThis.lng.hero_9);
        if (call.req.idArr.includes(call.req.id)) return call.error(globalThis.lng.hero_10);
        if (Object.values(call.conn.heroPos).concat(call.conn.gud.helpHeros.map(h => h._id)).intersection(call.req.idArr).length > 0) return call.error(globalThis.lng.hero_11);

        let check = needHero.map(v => {
            return new Array(v.n).fill(1).map(v1 => {
                return v.t;
            });
        }).reduce((a, b) => a.concat(b));

        if (call.req.idArr.length != check.length) return call.error(globalThis.lng.hero_12);

        let selectHeros: ResGetList['list'][''][] = [];
        for (let _id of call.req.idArr) {
            let _hero = await HeroFun.getHero(call, _id);
            if (!_hero) return call.error(globalThis.lng.hero_1);
            selectHeros.push(_hero);
        }

        let val1 = PublicShared.eval(check.join('+'));
        let val2 = PublicShared.eval(selectHeros.map(v => v.heroId).join('+'));

        if (val1 != val2) return call.error(globalThis.lng.hero_13);
        await PlayerFun.cutHero(call, call.req.idArr);
    }

    await PlayerFun.cutNeed(call, needItem);
    await HeroFun.changeHeroAttr(call, hero, {
        jieji: hero.jieji + 1
    });

    Wjjl.setVal(call.uid, `has_hero_jieji_${hero.jieji}`, 1, false);

    let help = call.conn.gud.helpHeros.find(v => v._id == call.req.id);
    if (help) {
        help.jieji = hero.jieji + 1;
        await PlayerFun.addAttr(call, { helpHeros: call.conn.gud.helpHeros });
    }

    event_cszl_record(call.conn.gud, hero.heroId, hero.jieji, 'jieji');

    call.succ({});
}