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


const { ccclass, property } = _decorator;


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



@ccclass('Props1Action')
export class Props1Action extends Component {
    @property(Node)
    lock_img:Node ;

    @property({ type: Label })
    conis_label: Label;  

    start() {
        this.conis_label.string = Global.get_need_coins() + "";
        this.initAdForPage();
    }


    /**
     * 初始化广告
     */
    initAdForPage(){
        console.log('zh:initAdForPage foir props1Action.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:initAdForPage 2')
    }


    dt:number = 0;
    times:number = 0;
    update(deltaTime: number) {
        if(!this.node.active){
            return;
        }
        if(this.times==0){
            this.times++;
            this.show_tween();
        }else{
            this.dt+=deltaTime;
            if(this.dt>=1.5){
                this.dt = 0;
                this.show_tween();
            }
        }
    }

    private show_tween(){    
        if(this.lock_img){
            this.lock_img.active = true;
            tween(this.lock_img)
            .to(0.25,{scale:new Vec3(1.2,1.2,1)},Global.our_easing)
            .to(0.35,{scale:new Vec3(0.8,0.8,1)},Global.our_easing)
            .to(0.15,{scale:new Vec3(1,1,1)},Global.our_easing)
            .call(()=>{
                this.lock_img.scale = new Vec3(1,1,1);
                this.lock_img.active = false;
            }).delay(0.3)
            .start();
           }
    }


    add_hole_coins(){
        console.log('zh:add_hole_coins111')
        AudioMgr.ins.playSound(Clips.btn_1);
        if (Global.layer_empty_action.get_unlock_num() <= 0) {
            Global.tips_action.show("There are no empty nail holes");//没有空位了
            return;
        }
        if (Global.cur_coins < Global.get_need_coins()) {
            Global.tips_action.show("gold Quantity Insufficient");//钻石数量不够
            return;
        }

        if (!DjManger.use_dj(DJ.Btn_1,DJ_TYPE.Type_coin)) {
            Global.tips_action.show("Current Level: Props Exhausted");//当前关卡次数已经用完   
            return;
        }
        //消耗
        Global.use_coins(Global.get_need_coins());
        Global.layer_empty_action.unlock_empty_hole();
        Global.coins_action.refrush_coins();

        this.node.parent.getComponent(PropsAction)?.close();
    }

    add_hole_videos(){
        console.log('zh:add_hole_videos222')
        AudioMgr.ins.playSound(Clips.btn_1);
        if (Global.layer_empty_action.get_unlock_num() <= 0) {
            Global.tips_action.show("no empty nail holes");//没有空位了
            return;
        }

        if (!DjManger.use_dj(DJ.Btn_1,DJ_TYPE.Type_video)) {
            Global.tips_action.show("Current Level: Props Exhausted");//当前关卡次数已经用完   
            return;
        }

        if(sys.os == sys.OS.ANDROID){
            if(ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementId())){
                console.log('zh:add_hole_videos ad  ok')
                sys.localStorage.setItem('yxAdMark', 'addHole');//看广告用于 添加钉子孔 标记
                ATRewardedVideoSDK.showAd(AAJS2.getPlacementId());
            }else{
                console.log('zh:add_hole_videos ad  no ok')
                
                Global.tips_action.show("No reward video for now");//没有奖励视频
                //this.useCoinsForAdPlayEnd();
            }
        }


        

        //下面是原始的逻辑
        // AdManger.show_video((data) => {
        //     if (data == 1) {
        //         console.log("add_hole_videos 获取奖励成功");
        //         this.node.parent.getComponent(PropsAction)?.close();
        //         //解锁
        //         Global.layer_empty_action.unlock_empty_hole();
        //     } else {
        //         Global.tips_action.show("Gain reward failure");//获取奖励失败
        //         //退款
        //         DjManger.return_used_dj(DJ.Btn_1,DJ_TYPE.Type_video);
        //     } 
        // })
        
    }


    /**
     * 消耗钻石  ad 播放完毕  消耗钻石
     */
    useCoinsForAdPlayEnd(){
        console.log("zh:add_hole_videos 开始添加钉子孔 奖励start");
        this.node.parent.getComponent(PropsAction)?.close();
        //解锁
        Global.layer_empty_action.unlock_empty_hole();
        console.log("zh:add_hole_videos 开始添加钉子孔 奖励end");
        //清理AD奖励标记
        sys.localStorage.removeItem('yxAdMark')
    }




    open(){
        this.conis_label.string = Global.get_need_coins() + "";
    }

}

