import { ApiCall } from "tsrpc";
import { HeroFun } from "../../public/hero";
import { ReqWear, ResWear } from "../../shared/protocols/heroskin/PtlWear";
import HeroSkinFun from "../../public/heroskin";

export default async function (call: ApiCall<ReqWear, ResWear>) {
    let hero = await HeroFun.getHero(call, call.req.heroOid);
    if (!hero) { // 英雄不存在
        call.error(lng.hero_1);
        return;
    }

    let skin = HeroSkinFun.getHeroSkin(call);
    if (!skin || !skin[call.req.skid]) { // 皮肤不存在
        call.error(lng.hero_20);
        return;
    }

    let skinconf = G.gc.heroSkin[call.req.skid];
    if (skinconf.heroId != hero.heroId) { // 皮肤不属于该英雄
        call.error(lng.hero_21);
        return;
    }

    await HeroFun.changeHeroAttr(call, hero, { skin: call.req.skid });

    call.succ({});
}