import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../../public/player';
import { ReqUpSkill, ResUpSkill } from "../../../shared/protocols/gongyu/xunlianjihua/PtlUpSkill";
import { PlayerShared } from '../../../shared/public/player';

export default async function (call: ApiCall<ReqUpSkill, ResUpSkill>) {

    let id = call.req.skillId;
    let conf = G.gc.xunlianjihua[id];
    let allPoints = (G.gc.playerLv[call.conn.gud.lv].tujianlv + (PlayerShared.getMingDaoBuff(call.conn.gud, 'zjgy_xld') || 0));
    let points = allPoints - call.conn.gud.useTujianLvPoint;
    let skills = Object.assign({}, call.conn.gud.skills);

    if (!conf) return call.error(globalThis.lng.gongyu_1);
    if (call.conn.gud.useTujianLvPoint < conf.cond) return call.error(globalThis.lng.gongyu_2);
    if (conf.front[0] && (!skills[conf.front[0]] || skills[conf.front[0]] < conf.front[1])) return call.error(globalThis.lng.gongyu_3);
    if (skills[id] >= conf.lv) return call.errorCode(-1);
    if (points < conf.need) return call.errorCode(-2);

    if (!skills[id]) skills[id] = 1;
    else skills[id]++;

    await PlayerFun.addAttr(call, { skills: skills, useTujianLvPoint: call.conn.gud.useTujianLvPoint + conf.need });

    call.succ({});
}