
import { Data } from "../../../mk/data/Data";
import JsbSystem from "../../../mk/system/JsbSystem";
import { GameProp } from "../GameData";
/**
 * 红包提现数据
 * - 跨Index不可提现，提示：请按顺序提现。
 * @author 薛鸿潇
 */
export class RedBagCashData extends Data {

    /**
     * 数据初始化
     */
    public init() {
        // 提现进度
        // this.cash_bar = gData.gameData.getProp(GameProp.redBag_cash_bar);// 自定义测试数据
        this.cash_bar = gData.gameData.gameData.cashIndex;// 服务端数据
        // this.initCData();// 假数据列表
        // 签到配置表
        if (gData.gameData.configs.CashCfg) {
            gData.gameData.configs.CashCfg.forEach(item_data => {
                for (const key in item_data) {
                    item_data[key] = parseInt(item_data[key])
                }
            });
            this.c_data = gData.gameData.configs.CashCfg;
            this.sortList();
            this.init_list = true;
        }
    }
    /** 标志位：初始化列表 */
    public init_list: boolean = false;

    /** 配置表数据 */
    public c_data: Array<CDataType> = [];

    private _cash_bar: number = 0;
    /** 提现进度 */
    set cash_bar(value: number) {
        this._cash_bar = value;
        this.init_list = true;
    }
    get cash_bar() {
        return this._cash_bar;
    }

    /**
     * 提现操作
     */
    public async cashOP() {
        if (!gData.loginData.isAuth) {
            JsbSystem.WxAuth();
            return;
        }
        /** 提现档位 */
        const cash_index = this.cash_bar;
        let data = { index: cash_index };
        let response = await mk.http.sendData('cashUsual', data);
        if (response.errcode != 0) {
            mk.tip.pop(response.errmsg);
            return;
        }
        this.cash_bar++;
        // gData.gameData.setProp(GameProp.redBag_cash_bar, this.cash_bar);
        gData.gameData.gameData.cashIndex++;

        // 扣除红包
        this.sortList();
        let item_data = this.getItemDataByIndex(cash_index);
        gData.gameData.gameData.redMoney -= item_data.type_value;

        // 打开提现到账界面
        gData.receiptNotice.receip_rmb = item_data.money;
        gData.cashNormal.receip_total_rmb += gData.receiptNotice.receip_rmb;
        mk.ui.openPanel('module/receiptNotice/receiptNotice');

    }

    /**
     * 排序
     * - 已提条目放最后
     */
    private sortList() {
        const l_count = this.c_data.length;
        let index = 0;
        for (let i = 0; i < l_count; i++) {
            if (this.c_data[index].index <= gData.redBagCash.cash_bar) {
                let item_data = this.c_data.splice(index, 1);
                this.c_data.push(item_data[0]);
                index--;
            }
            index++;
        }
    }


    /**
     * 获取条目，通过index
     * @param cash_index 对应id
     * @returns 
     */
    public getItemDataByIndex(cash_index: number): CDataType {
        const l_count = this.c_data.length;
        for (let i = 0; i < l_count; i++) {
            if (this.c_data[i].index === cash_index) {
                return this.c_data[i]
            }
        }
    }

    /** 客户端造的假数据 */
    private initCData() {
        for (let i = 0; i < 20; i++) {
            const id = i + 1;
            let obj = {
                index: id,
                /** 毛币数量？ */
                money: id,
                /** 红包数量 */
                type_value: id * 1000,
                time: id,
                directVideoTime: id,
                delayVideoTimes: id,
                money_type: id,
                num: id,
                moneyshow: id,
                summoney: id
            };
            this.c_data.push(obj)
        }
        this.init_list = true;
    }
}
/** 配置表数据内容 */
export type CDataType = {
    /** id */
    index: number,
    /** 毛币数量，单位分 */
    money: number,
    /** 红包币数量 */
    type_value: number,
    time: number,
    directVideoTime: number,
    delayVideoTimes: number,
    money_type: number,
    num: number,
    moneyshow: number,
    summoney: number
}

