import JsbSystem from "../../../mk/system/JsbSystem";
import TableView from "../../component/TableView";
import { AdFun } from "../../data/AdData";
import { RewardType } from "../../data/GameData";

/**
 * 签到界面
 * - 每天第一次上线默认弹出一次【除首日】。待处理
 * - 全部签完后，不可再签到。此处需要判断上次签到天数。
 * - 滑动列表需要滚动到对应签到条目的高度，待处理。
 * @author 薛鸿潇
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class Sign extends cc.Component {

    @property({ type: cc.ScrollView, displayName: '滚动视图' })
    private scroll_view: cc.ScrollView = null!;

    @property({ type: cc.Animation, displayName: '按钮_动画' })
    private anim_btn: cc.Animation = null!;

    private tableview: TableView = null;
    onLoad() {

    }

    start() {
        mk.ad.showNativeAd(4);
        // gData.sign.sign_can = gData.gameData.getProp(GameProp.sign_can);
        gData.sign.initListData();
        gData.sign.init_data = true;
        this.initScollView();
    }

    /**
     * 初始化滚动视图
     */
    private initScollView() {
        let list_data = gData.sign.list_data;
        this.scroll_view.node.emit('srollview-init', list_data)
        this.tableview = this.scroll_view.node.getComponent('TableView')

        let index = gData.sign.getItemIndexByCanSign();
        this.tableview.contentMoveByIndex(index)
    }


    update(dt) {
        if (gData.sign.init_data) {
            this.initBtnStyle();
            return;
        }
        if (gData.sign.adData) {
            // 展示奖励
            this.getReward();
        }
    }

    lateUpdate() {
        gData.sign.init_data = false;
        gData.sign.adData = null;
    }

    /**
     * 点击领取按钮
     */
    private clickReceiveReward() {
        mk.audio.playEffect("button");
        if (gData.gameData.gameData.isSignInToday == 1) return;
        if (!gData.sign.haveSignDay()) return;

        const index_old = gData.sign.getItemIndexByCanSign();
        if (gData.sign.c_sign[index_old].rewardType === RewardType.rmb) {
            if (!gData.loginData.isAuth) {
                JsbSystem.WxAuth();
                return;
            }
        }
        // 看广告
        mk.ad.watchAd((success: boolean) => {
            mk.console.log("watchAD:" + success);
            if (success) {
                gData.adData.watchVideo(AdFun.sign);
            }
        });
    }

    /**
     * 拿奖励
     */
    private getReward() {
        const index_old = gData.sign.getItemIndexByCanSign();
        // 完成展示
        gData.sign.signComplete();

        // 指定条目数据刷新
        this.tableview.resetItemData(index_old, gData.sign.list_data[index_old]);

        // 打开奖励界面
        // to do
    }

    /**
     * 初始化按钮样式
     */
    private initBtnStyle() {
        if (gData.gameData.gameData.isSignInToday == 0 && gData.sign.haveSignDay()) {
            this.anim_btn.play();
            this.anim_btn.getComponent('SetGray').setGray(false);
        } else {
            this.anim_btn.stop();
            this.anim_btn.node.scale = 1;
            this.anim_btn.getComponent('SetGray').setGray(true);
        }
    }

    onClickClose() {
        mk.ad.destroyNativeAd();
        mk.ad.checkShowInterByChance();
    }
}
