import { ApiCall } from "tsrpc";
import { PlayerFun } from "../../public/player";
import { ReqUplv, ResUplv } from "../../shared/protocols/heroskin/PtlUplv";
import HeroSkinFun from "../../public/heroskin";


export default async function (call: ApiCall<ReqUplv, ResUplv>) {
    let skin = HeroSkinFun.getHeroSkin(call);
    if (!skin[call.req.skid]) {// 皮肤未获得
        call.error(lng.hero_20);
    }

    let cur_lv = skin[call.req.skid];
    let skinconf = G.gc.heroSkin[call.req.skid];

    let need = [];
    for (let i = 0; i < call.req.lv; i++) {
        need = need.concat(
            G.gc.heroSkinLv[skinconf.colour][cur_lv + i].need
        )
        let conf = G.gc.heroSkin[call.req.skid];
        need.push({ a: "item", "t": conf.heroId, "n": G.gc.heroSkinLv[skinconf.colour][cur_lv + i].suipian });
    }

    // 检测消耗
    await PlayerFun.checkNeedIsMeet(call, need);

    // 扣除消耗
    await PlayerFun.cutNeed(call, need);

    // 更新等级
    skin[call.req.skid] = cur_lv + call.req.lv;

    // 更新skinlv
    await HeroSkinFun.updateHeroSkinLv(call, { [call.req.skid]: cur_lv + call.req.lv });

    call.succ({});
}