import { _decorator, Color, Component, Label, Node, Sprite, tween, Vec2, Vec3,sys } from 'cc';

import { Global } from './Global';
import { PropsAction } from './PropsAction';
import { Clips } from './Enums';
import { AudioMgr } from './AudioMgr';
import { CatAction } from './CatAction';
import { DataModel } from './DataModel';
import { AdManger } from './ad/AdManger';
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('Props4Action')
export class Props4Action extends Component {

    @property(Node)
    target:Node = null;

    @property(Sprite)
    cat_img:Sprite = null;

    color_arr = [];

    color_arr_index = 0;

    cat_action:CatAction

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

    start() {
        this.conis_label.string = Global.get_need_coins() + "";
        for(let i = 0;i<DataModel.base_color_arr.length;i++){
            this.color_arr.push(DataModel.base_color_arr[i].clone());
        }
        this.initAdForPage();
    }
    initAdForPage(){
        console.log('zh:initAdForPage foir props3Action.ts')
        if(sys.os===sys.OS.ANDROID){
            let deviceId = AAJS2.getDeviceUserId();
            console.log("zh:checkstatus:",ATRewardedVideoSDK.checkAdStatus(AAJS2.getPlacementId()));
            var setting = {};
            setting[ATRewardedVideoSDK.userIdKey] = deviceId;
            ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementId(), setting);
        }
    }

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

    private play_tween(){
        this.color_arr_index++;
        if(this.color_arr_index>=this.color_arr.length){
            this.color_arr_index = 0;
        }
        let c = this.color_arr[this.color_arr_index];
        this.cat_img.color = new Color(c.r,c.g,c.b,255);
    }

   
    show_box(cat_action:CatAction){
        this.cat_action = cat_action;
    }

    /**
     * 开启一个盒子
     * @returns 
     */
    open_box_coins(){
        AudioMgr.ins.playSound(Clips.btn_1);

        if(Global.cur_coins<Global.get_need_coins()){
            Global.tips_action.show("Gold Quantity Insufficient");//钻石数量不够
            return;
        }

         //消耗
         Global.use_coins(Global.get_need_coins());
         {
            //解开
            this.cat_action.set_lock_unlock(false);
            this.cat_action.into_scence(new Vec2(0,400));
         }
         Global.coins_action.refrush_coins(); 

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

    /**
     * 开启盒子
     * @returns 
     */
    open_box_video(){
        AudioMgr.ins.playSound(Clips.btn_1);
        if(!this.cat_action){
            return;
        }
        //检查上锁情况
        // if (!Global.used_cur_level_props_(4)) {
        //     Global.tips_action.show("当前关卡次数已经用完");
        //     return;
        // }


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


        //下面是原始的代码
        // AdManger.show_video((data) => {
        //     if (data == 1) {
        //         console.log("open_box_video 获取奖励成功");
        //         this.node.parent.getComponent(PropsAction)?.close();
        //         //解锁
        //         this.cat_action.set_lock_unlock(false);
        //         this.cat_action.into_scence(new Vec2(0,400));
        //     } else {
        //         Global.tips_action.show("获取奖励失败5");
        //         //退款
        //         // Global.return_used_cur_level_props_(4);
        //     } 
        // })

        
       
    }


        /**
     * 消耗钻石  ad 播放完毕  消耗钻石
     */
    useCoinsForAdPlayEnd(){
        console.log("zh:clear_pins_videos 开始openBox 奖励start");
       
        this.node.parent.getComponent(PropsAction)?.close();
        this.cat_action.set_lock_unlock(false);
        this.cat_action.into_scence(new Vec2(0,400));
        console.log("zh:clear_pins_videos 开始openBox 奖励end");
        //清理AD奖励标记
        sys.localStorage.removeItem('yxAdMark')
    }

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

}

