import {ApiCall} from "tsrpc";
import {PlayerFun} from '../../public/player';
import {ReqChangeInfo, ResChangeInfo} from "../../shared/protocols/user/PtlChangeInfo";
import {ResLogin} from '../../shared/protocols/user/PtlLogin';
import {UserShared} from '../../shared/public/user';

export default async function (call: ApiCall<ReqChangeInfo, ResChangeInfo>) {

    // if (call.req.type == 'model' && !call.req.pos) return call.error(globalThis.lng.shiwu_6);

    let active = (await G.mongodb.collection('playerInfo', 'playerInfo').findOne({
        uid: call.uid,
        type: 'playerInfo'
    }))?.active || {};
    const fmtId = UserShared.getInfoId(call.req.type, call.req.id);
    if (R.values(active).length == 0) active = UserShared.getActive(fmtId, call.conn.gud, call.req.type, call.req.id)

    const isMeet = UserShared.chechIsActive(fmtId, {
        lv: call.conn.gud.lv,
        vip: call.conn.gud.vip,
        lsyx: call.conn.lshd.hero,
        model: call.conn.gud.model,
        active: active,
        heroskin: call.conn.gud.heroskin
    });

    if (!isMeet && call.req.type != 'headFrame' && call.req.type != 'chatFrame') return call.error(globalThis.lng.user_1);

    const change: Partial<ResLogin['gud']> = {};

    if (call.req.type == 'model') {
        let d = JSON.parse(JSON.stringify(call.conn.gud.model));
        let changeItem = {
            id: call.req.id,
            time: active[fmtId] || -1
        }
        d[call.req.id] = changeItem
        change.upModel = changeItem
        change.model = d;
    } else {
        change[call.req.type] = {
            id: call.req.id,
            time: active[fmtId] || -1
        };
    }

    await PlayerFun.addAttr(call, change);

    call.succ({});
}