import { _decorator, Component, instantiate, Label, Node, tween, UITransform, Vec3 ,sys} from 'cc';
import { Global } from './Global';
import { Clips } from './Enums';
import { AudioMgr } from './AudioMgr';
import { BasePage } from './BasePage';
import { AdManger } from './ad/AdManger';
import { Tools } from './Tools';
import { PoolMgr } from './PoolMagr';


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('SuccessAction')
export class SuccessAction extends BasePage {

    @property({type:Label})
    level_label:Label = null;

    @property({type:Label})
    coins_label:Label = null;

    @property({type:Node})
    coins_img:Node = null;

    @property({type:Node})
    get_coins_btn:Node = null;

    start() {
        super.start();
        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')
    }




    update(deltaTime: number) {
        
    }

     //show menu plant
     open(){
        super.open();
        if(this.get_coins_btn){
            this.get_coins_btn.active = true;
        }
        AudioMgr.ins.playSound(Clips.complete_2);
        this.level_label.string = "Level "+ Global.cur_lvl+" ";

        this.coins_label.string = ""+ Global.cur_coins+" ";
        this.coins_label.node.scale = new Vec3(3,3,0);
        tween(this.coins_label.node)
        .to(0.5, { scale: new Vec3(1, 1,0) } ,Global.our_easing)
        .start();
    }

    btn_next(){
        AudioMgr.ins.playSound(Clips.btn_1);
        if(this.get_coins_btn){
            this.get_coins_btn.active = false;
        }
        this.close();
        AdManger.show_interstial();
        // Global.reset_level_default();
        Global.main_action.next_lvl();
    }

    private play_add_gift_tween(num:number,world_pos:Vec3,callBack:Function){
        let prefab = PoolMgr.ins.getPrefab("coin");
        let temp = 0; 
        for(let i =0;i<num;i++){
            let coin = instantiate(prefab);
            let local = this.coins_img.getComponent(UITransform).convertToNodeSpaceAR(world_pos);
            this.coins_img.addChild(coin);
            local.x = local.x + Tools.random_between(-30,80);
            local.y = local.y + Tools.random_between(0,150);
            coin.setPosition(local);
            tween(coin)
            .to(Tools.random_between(0.5,1.2),{position:new Vec3(0,0,0)},Global.our_easing)
            .call(()=>{
                temp++;
                this.coins_label.string = ""+ (Global.cur_coins+temp);
                AudioMgr.ins.playSound(Clips.coins);
                if(temp>=num){
                    if(callBack){
                        callBack();
                        callBack = null;
                    }
                    this.coins_label.string = ""+ Global.cur_coins;
                }
            })
            .removeSelf()
            .start();
        }
    }

    get_one_bag_coins(){
        if(this.get_coins_btn){
            this.get_coins_btn.active = false;
        }else{
            return;
        }
        AudioMgr.ins.playSound(Clips.btn_1);





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



        //下面是原始的
        // AdManger.show_video((data) => {
        //     if (data == 1) {
        //         console.log("get_one_bag_coins 获取奖励成功");
        //         let num = Math.ceil(Tools.random_between(15,46));
        //         // console.log(" random data num :",num);
        //         this.play_add_gift_tween(num,this.get_coins_btn.getWorldPosition(),()=>{
        //             Global.coins_action.put_coins(num,this.coins_img.getWorldPosition(),false);
        //         });
               
        //     } else {
        //         Global.tips_action.show("Gain reward failure");//获取奖励失败
        //         // this.close();
        //         // Global.main_action.next_lvl();  
        //     } 
        // })
    }

    
    /**
     * 
     */
    useCoinsForAdPlayEnd(){
        console.log("zh:get_one_bag_coins 开始发放更多钻石 奖励start");

        let num = Math.ceil(Tools.random_between(15,46));
                // console.log(" random data num :",num);
        this.play_add_gift_tween(num,this.get_coins_btn.getWorldPosition(),()=>{
            Global.coins_action.put_coins(num,this.coins_img.getWorldPosition(),false);
        });
        
        console.log("zh:get_one_bag_coins 开始发放更多钻石 奖励end");
      
        //清理AD奖励标记
        sys.localStorage.removeItem('yxAdMark')
    }


}

