import { Data } from "../../../mk/data/Data";
import { GameProp, RewardState, RewardType } from "../GameData";

/**
 * @description 签到数据
 * @author 邹勇 薛鸿潇
 */
export class SignData extends Data {
    /**
     * 数据初始化
     */
    public init() {
        // 签到配置表
        if (gData.gameData.configs.SignIn) {
            gData.gameData.configs.SignIn.forEach(item_data => {
                for (const key in item_data) {
                    item_data[key] = parseInt(item_data[key])
                }
            });
            this.c_sign = gData.gameData.configs.SignIn;
        }

        // 客户端测试数据
        // this.sign_last_bar = gData.gameData.getProp(GameProp.sign_last_bar);
        // this.sign_can = gData.gameData.getProp(GameProp.sign_can);
    }

    /** 标志位：是否需要刷新数据 */
    public init_data: boolean = false;

    /** 配置数据 签到 */
    private _c_sign: Array<CSignType> = [];
    set c_sign(value: Array<CSignType>) {
        this._c_sign = value;
        this.init_data = true;
    }
    get c_sign(): Array<CSignType> {
        return this._c_sign;
    }


    /** 签到列表数据 */
    public list_data: Array<Array<CSignType>> = [];
    /** 初始化列表数据 */
    public initListData() {
        // if (!this.c_sign.length) this.c_sign = this.getSignConfig();
        let list_data = [];
        for (let i = 0; i < 30;) {
            let item_data = [];
            for (let j = 0; j < 3; j++) {
                let data = this.c_sign[i];
                item_data.push(data);
                i++;
            }
            list_data.push(item_data);
        }
        this.list_data = list_data;
    }

    /** 客户端造的假配置 */
    // private getSignConfig() {
    //     let c_sign = [];
    //     for (let i = 0; i < 30; i++) {
    //         const id = i + 1;
    //         let data: any = {};
    //         data.id = id;
    //         data.reward_type = i % 3;
    //         data.count = id;
    //         data.login_day = id;
    //         data.video_limit = 0;
    //         c_sign.push(data);
    //     }
    //     return c_sign;
    // }

    /**
     * 获取奖励状态
     * @param login_day 登陆日 
     * @returns 状态123
     */
    public getStateByDay(login_day: number) {
        let _state = 0;
        if (login_day <= gData.gameData.gameData.signInDay) {
            _state = RewardState.none;
        } else if (gData.gameData.gameData.isSignInToday == 0 && login_day == (gData.gameData.gameData.signInDay + 1)) {
            _state = RewardState.unlock;
        } else {
            _state = RewardState.lock;
        }
        return _state;
    }

    /**
     * 存在可签到天数
     * - 签到天数用完后将不可签到
     */
    public haveSignDay() {
        if (gData.gameData.gameData.signInDay >= this.c_sign.length) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 签到完成
     */
    public async signComplete() {
        gData.gameData.gameData.signInDay++;
        gData.gameData.gameData.isSignInToday = 1;
        // // 自定义状态
        // let new_data = [
        //     {
        //         key: GameProp.sign_can,
        //         value: gData.gameData.gameData.isSignInToday
        //     },
        //     {
        //         key: GameProp.sign_last_bar,
        //         value: this.sign_last_bar
        //     }
        // ]
        // gData.gameData.setProps(new_data);
        // 奖励展示
        let reward_data = this.c_sign[gData.gameData.gameData.signInDay - 1];
        if (reward_data.rewardType === RewardType.redBag) {
            // 红包
            gData.reward.data = [reward_data];// 数据列表
            mk.ui.openPanel('module/reward/reward')
        } else if (reward_data.rewardType === RewardType.rmb) {
            // 现金提现
            let data = {};
            let response = await mk.http.sendRequest('readyCash', 'POST', JSON.stringify(data));
            // let response = await mk.http.sendRequest('readyCash', 'POST', JSON.stringify(data));
            if (response.errcode != 0) {
                mk.tip.pop('系统异常');
                return;
            }

            if (response.CashMode == 0) {//星云打款
                gData.receiptNotice.receip_rmb = reward_data.rewardNum;
                gData.cashNormal.receip_total_rmb += gData.receiptNotice.receip_rmb;
                mk.ui.openPanel('module/receiptNotice/receiptNotice');
            } else if (response.CashMode == 1) {//公众号打款

            }
        } else if (reward_data.rewardType === RewardType.pigRmb) {
            // 金猪现金
            gData.reward.data = [{ rewardNum: reward_data.rewardNum, rewardType: reward_data.rewardType }]
            mk.ui.openPanel('module/reward/reward');
        }

        // 获得奖励
    }

    /**
     * 获取数据列表中第一个可签到条目的index
     * @returns index
     */
    public getItemIndexByCanSign(): number {
        let index = 0;
        const l_count = this.list_data.length;
        for (let i = 0; i < l_count; i++) {
            const i_count = this.list_data[i].length;
            for (let j = 0; j < i_count; j++) {
                let state = this.getStateByDay(this.list_data[i][j].loginDay);
                if (gData.gameData.gameData.isSignInToday == 0) {
                    if (state === RewardState.unlock) {
                        index = i;
                        return index
                    }
                } else {
                    if (state === RewardState.lock) {
                        index = i;
                        return index
                    }
                }
            }
        }
        return index;
    }

}

export type CSignType = {
    id: number,
    /** 天数 */
    loginDay: number,
    /** 奖励数量 */
    rewardNum: number,
    /** 奖励类型 */
    rewardType: number,
    /** 视频限制 */
    videoNum: number,
}