import { ApiCall } from 'tsrpc';
import { hongdianVal } from '../shared/protocols/hongdian/PtlGet';
import { ctype } from '../shared/protocols/sign/type';
import { PublicShared } from '../shared/public/public';
import { PlayerFun } from './player';


export class SignFun {
    /**获取配置信息 */
    static async getCon() {
        let _con = G.gc.qiandao
        return _con
    }
    /**获取宝箱配置 */
    static async getBoxCon() {
        let _con = G.gc.qiandaocom
        return _con
    }

    /**初始数据 */
    static async initData() {
        let _nt = G.time
        let _r = {
            refreshtime: _nt,
            gotarr: [],
            login: 1,
            logintime: _nt,
            boxgotarr: []
        }
        return _r
    }

    /**获取签到信息 */
    static async getInfo(uid: string, where: { ctype: string }) {
        let info: {} = await PlayerFun.getAttrOne(uid, where)
        const _con = await this.getCon()
        let _isSet = 0
        let _nt = G.time
        if (!info) {
            info = await this.initData()
            info["key"] = "1"
            _isSet = 1
        }

        // 是否到下月
        let _diff = PublicShared.getDiff(info["refreshtime"])
        if (_diff > Object.keys(_con[info["key"]]).length) {
            Object.assign(info, await this.initData())
            info["key"] = await this.getNewMonth(info)
            _isSet = 1
        }

        // 设置数据
        if (_isSet == 1) {
            this.setInfo(uid, info)
        }
        return info
    }

    /**更换月份key */
    static async getNewMonth(info) {
        const _con = await this.getCon()
        let _keys = Object.keys(_con)
        let _idx = _keys.indexOf(info.key)
        let _res: string
        if (_idx + 1 >= _keys.length) {
            _res = _keys[0]
        } else {
            _res = _keys[_idx + 1]
        }
        return _res
    }

    /**更新登录信息 按照实际登录来算*/
    static async updateLogin(uid: string) {
        let _info = await this.getInfo(uid, { ctype: ctype })
        if (!PublicShared.chkSameDate(_info["logintime"], G.time)) {
            _info["login"] += 1
            _info["logintime"] = G.time
            this.setInfo(uid, _info)
        }
    }

    /**设置数据 */
    static async setInfo(uid: string, data: {}) {
        PlayerFun.setAttr(uid, { ctype: ctype }, data)
    }

    /**红点 */
    static async getHongDian(call: ApiCall) {
        let _res: hongdianVal = {
            show: false
        }

        let [isOpen, tips] = PublicShared.getOpenCond(call, "zc_img6")
        if (!isOpen) return _res;

        let info = await this.getInfo(call.uid, { ctype: ctype })
        // 登录奖励
        if (info["login"] > info["gotarr"].length) {
            _res.show = true
            return _res
        }
        // 宝箱奖励
        const _boxCon = await this.getBoxCon()
        for (let i in _boxCon) {
            if (parseInt(i) <= info["login"] && info["boxgotarr"].indexOf(i) == -1) {
                _res.show = true
                return _res
            }
        }
        return _res
    }
}