import { _decorator, Component, director, Node, Sprite, tween, Vec3, Widget, sys } from 'cc';
import { AudioMgr } from './AudioMgr';
import { Clips, DJ, DJ_TYPE } from './Enums';
import { Global } from './Global';
import { BasePage } from './BasePage';
import { AdManger } from './ad/AdManger';
import { DjManger } from './DjManger';




import ATRewardedVideoSDK from "./AnyThinkAds/ATRewardedVideoTSSDK"
import AAJS2 from "./utils/ATAndroidJS2";
import ATJSSDK from "./AnyThinkAds/ATJSSDK";
import { CoinsAction } from './CoinsAction';
//import { EncryptUtil } from './utils/EncryptUtil';


const { ccclass, property } = _decorator;

@ccclass('GameOverAction')
export class GameOverAction extends BasePage {
    @property({ type: Node })
    fail_img: Node
    @property({ type: Node })
    fail_dou: Node

    @property({ type: Node })
    btn_continue: Node

    start() {
        super.start();
        this.initAdForPage();
    }


    /**
     * 
     */
    initAdForPage() {
        console.log('zh:initAdForPage for GameOverAction.ts')
        if (sys.os === sys.OS.ANDROID) {
            console.log('zh:initAdForPage 1')
            let deviceId = AAJS2.getDeviceUserId();
            console.log("zh:checkstatus:", ATRewardedVideoSDK.checkAdStatus(AAJS2.getPlacementId()));
            var setting = {};
            setting[ATRewardedVideoSDK.userIdKey] = deviceId;
            ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementId(), setting);
        }
        console.log('zh:GameOverAction 2')
    }

    update(deltaTime: number) {

    }

    
    /**
     * show menu panel
     * @returns 
     */
    open() {
        if (this.node.position.x == 0 && this.node.position.y == 0) {
            //console.log("打开中，跳过");
            return;
        }
        super.open();
        this.fail_img.scale = new Vec3(2, 2, 0);
        tween(this.fail_img)
            .to(0.3, { scale: new Vec3(1, 1, 0) }, Global.our_easing)
            .call(() => {
            })
            .start();

        this.fail_dou.scale = new Vec3(0.1, 0.1, 0);
        tween(this.fail_dou)
            .to(0.3, { scale: new Vec3(1, 1, 0) }, Global.our_easing)
            .call(() => {
            }).start();

        AudioMgr.ins.playSound(Clips.fail);
    }

    /**
     * go to home page
     */
    btn_go_home() {
        this.close();
        director.loadScene("home");
        AdManger.show_interstial();
    }

    /**
     * restart game
     */
    btn_restart_game() {
        this.close();
        Global.main_action.restart_game();
        AdManger.show_interstial();
    }


    /**
     * 
     * @returns 
     */
    clear_pins_videos() {
        console.log('zh:失败了，我要复活。。。')
        AudioMgr.ins.playSound(Clips.btn_1);
        let pin_arr = Global.layer_empty_action.get_pin_arr();
        if (pin_arr.length == 0) {
            Global.tips_action.show("No Screws to Clear");//没有可清理的钉子
            return;
        }
        if (!DjManger.use_dj(DJ.Btn_5, DJ_TYPE.Type_video)) {
            Global.tips_action.show("Current Level: Props Exhausted");//当前关卡次数已经用完   
            return;
        }
        // Global.log_dj();
        // this.close();
        // Global.log_dj();
        if (sys.os == sys.OS.ANDROID) {
            if (ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementId())) {
                console.log('zh:继续 ad  ok')
                sys.localStorage.setItem('yxAdMark', 'fuHuo');//看广告用于 继续 标记
                ATRewardedVideoSDK.showAd(AAJS2.getPlacementId());
            } else {
                console.log('zh:继续 ad  no ok')

                Global.tips_action.show("No reward video for now");//没有奖励视频
                //this.useCoinsForAdPlayEnd();
            }
        }

        //下面是原始的
        // AdManger.show_video((data) => {
        //     if (data == 1) {
        //         console.log("clear_pins_videos 获取奖励成功");
        //         this.close();
        //         //解锁
        //         Global.bucket_action.put_pins(pin_arr);
        //     } else {
        //         Global.tips_action.show("Gain reward failure");//获取奖励失败
        //         //退款
        //         DjManger.return_used_dj(DJ.Btn_5,DJ_TYPE.Type_video);
        //     } 
        // })

    }


    /**
     * 消耗钻石  ad 播放完毕  消耗钻石
     */
    useCoinsForAdPlayEnd() {
        console.log("zh:yxContinue 继续 奖励start");
        this.close();
        let pin_arr = Global.layer_empty_action.get_pin_arr();
        if (pin_arr.length == 0) {
            Global.tips_action.show("No Screws to Clear");//没有可清理的钉子
            return;
        }
        //解锁
        Global.bucket_action.put_pins(pin_arr);
        console.log("zh:yxContinue 继续 奖励end");
        //清理AD奖励标记
        sys.localStorage.removeItem('yxAdMark')
    }


}

