import { ApiCall } from 'tsrpc';
import { weixiuchangType } from "../shared/protocols/weixiuchang/type";
import { hongdianVal } from '../shared/protocols/hongdian/PtlGet';
import { PlayerFun } from "../public/player";

export class FunWeiXiuChang {
    static defaultSchedule = [0,0,0,0,0]
    static async getFindOne (call: ApiCall): Promise<weixiuchangType>{
        let uid = call.uid
        let data:weixiuchangType = await G.mongodb.collection('weixiuchang').findOne({uid});
        if(!data) {
            data = {
                uid,
                schedule: this.defaultSchedule,
                star: 0,
                stage: 1,
                utime: G.time
            }
            G.mongodb.collection('weixiuchang').insertOne(data);
            await PlayerFun.addAttr(call, { wxcLv: {lv: data.star, ids: data.schedule} });
        }
        return data
    }

    static async updateOne(call: ApiCall, data: weixiuchangType) {
        await PlayerFun.addAttr(call, { wxcLv: {lv: data.star, ids: data.schedule} });
        return G.mongodb.collection('weixiuchang').updateOne({uid: call.uid}, {$set: data});
    }

    // 红点-维修厂
    static async getHongDian(call: ApiCall):Promise<hongdianVal> {
        let res:hongdianVal = { show: false };
        let { star, schedule } = await this.getFindOne(call);
        let { need } = G.gc.weixiuchang_zz[star]; // 升级需要的零件数量配置
        // 判断是否能升级|升星
        let starCount = 0
        for(let k = 0; k < need.length; k++) {
            let item = need[k]
            // 零件已装满, 升星条件累计
            if(item.n == schedule[k]) starCount++
            else {
                // 物品数量判断
                let itemNum = await PlayerFun.getAtnNum(call, item); //获取物品数量
                if(itemNum > 0) {
                    res.show = true;
                    break;
                }
            }
        }
        if(starCount == need.length) res.show = true;
        return res
    }
}