import { find, instantiate, macro, Node, ScrollView, sp, Sprite, tween, Tween, UITransform, v2, _decorator ,sys} from 'cc';
import { BaseLayer } from '../../common/BaseLayer';
import { cocosUtil } from '../../utils/cocosUtil';
import { utilTools } from '../../utils/utilTools';
import { constants } from '../data/constants';
import { msgac } from '../data/msgac';
import { ItemLayer } from '../item/ItemLayer';
import { designManager } from '../manager/designManager';
import { eventManager } from '../manager/eventManager';
import { iconManager } from '../manager/iconManager';
import { levelManager } from '../manager/levelManager';
import { sdkManager } from '../manager/sdkManager';
import { playerModel } from '../model/playerModel';
import { FlutterEffect } from './effect/FlutterEffect';
import { ReelEffect } from './effect/ReelEffect';
import { FightLayer } from './FightLayer';



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

const { ccclass, property } = _decorator;

@ccclass('LevelLayer')
export class LevelLayer extends BaseLayer {

    /**
     * AD播放完毕标识
     */
    private adPalyOverEve_ = 'adPalyOverEve';
    //AD播放完毕后需要进行什么奖励的标记，如:fuHuo等,启用游戏的时候需要清空该KEY
    private yxAdMark = 'yxAdMark';

    listScrollView: ScrollView;
    listContentLayer: Node;
    levelLayer: Node;
    playerLayer: Node;
    skillEffectLayer: Node;

    hitEffectLayer: Node;

    btnAdAtk: Node;
    btnSkip: Node;

    levelNode: Node;
    level: number;

    handNode: Node;
    skillLayer: Node;
    fightLayer: Node;

    cloundLayer1: Node;
    cloundLayer2: Node;

    onLoad() {
        console.log("zh:LevelLayer onLoad");
        super.onLoad();

        this.getNodeByPath("cloundLayer").addComponent(FlutterEffect);
        this.getNodeByPath("moutainLayer").addComponent(ReelEffect);

        this.cloundLayer1 = this.getNodeByPath("list/view/content/cloundLayer1");
        this.cloundLayer1.addComponent(FlutterEffect).speed = 70;
        this.cloundLayer2 = this.getNodeByPath("list/view/content/cloundLayer2");
        this.cloundLayer2.addComponent(FlutterEffect).speed = 80;

        this.levelNode = this.getNodeByPath("level/level");
        this.btnAdAtk = this.getNodeByPath("btnAdAtk");
        this.btnSkip = this.getNodeByPath("btns/btnSkip");

        this.listScrollView = this.getNodeByPath("list").getComponent(ScrollView);
        this.listContentLayer = this.getNodeByPath("list/view/content");
        this.levelLayer = this.getNodeByPath("list/view/content/levelLayer");
        this.playerLayer = this.getNodeByPath("list/view/content/playerLayer");
        this.skillEffectLayer = this.getNodeByPath("list/view/content/skillEffectLayer");

        this.hitEffectLayer = this.getNodeByPath("hitEffectLayer");
        this.hitEffectLayer.addComponent(ItemLayer);

        this.skillLayer = this.getNodeByPath("skillLayer");
        this.skillLayer.addComponent(ItemLayer);

        this.fightLayer = this.getNodeByPath("fightLayer");
        this.fightLayer.addComponent(FightLayer);
        this.fightLayer.active = false;

        this.level = this.obj.level;

        this.initUI();

      //  this.initAd();
       // this.initAdForPage();

    }

       /**
     * 初始化AD
     */
       initAd() {

        //1、清理AD奖励标记
        sys.localStorage.removeItem('yxAdMark')

        console.log("zh:initAd start");
        // // 初始化SDK  
        //ATJSSDK.initSDK("h67122e47607cd", "a4fd9a23bdd71c2314cf41140a77abb1e");//这是cccc线上的
        ATJSSDK.initSDK("h67370c81d408e", "a0c9895b9687e4978a3ed2f2c0f441633");//正式的
        ATJSSDK.setLogDebug(true);
        // // 针对欧盟地区初始化时做的处理，按需求打开，不在欧盟地区发布的不用使用。
        ATJSSDK.getUserLocation(function (userLocation) {
            //如果处于欧盟地区且等级是UNKNOW时，就执行授权弹窗
            if (userLocation === ATJSSDK.kATUserLocationInEU) {
                if (ATJSSDK.getGDPRLevel() === ATJSSDK.UNKNOWN) {
                    ATJSSDK.showGDPRAuth();
                }
            }
        });
        //var state = false;
        ATRewardedVideoSDK.setAdListener(this);
        if (sys.os === sys.OS.ANDROID) {
            let deviceId = AAJS2.getDeviceUserId();
            //console.log("checkstatus:",ATRewardedVideoSDK.checkAdStatus(AAJS2.getPlacementId()));
            var setting = {};
            setting[ATRewardedVideoSDK.userIdKey] = deviceId;
            ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementId(), setting);
        }

        if (sys.platform == sys.Platform.ANDROID) {
            setTimeout(() => {
                var allInfo = AAJS2.allInfo();
                console.log('zh:allInfo=' + allInfo)
                let key = "US2%*c3lv8sYkUe(!e-6g$E*RJg)dzn@";
                let iv = "Jn0.aWsOu$y-Dbqb";

                // let data = { data: AESUtil.AESencrypt(allInfo, key, iv) };
                // ATSDK.printLog("zh:allInfo-jm=" + JSON.stringify(data));
                // console.log('zh:ad_allInfo', data);
                // AAJS2.makePostRequestWithXhr(JSON.stringify(data));


                // let data = { data: EncryptUtil.aesEncrypt(allInfo, key, iv) };
                // console.log("zh:allInfo jm=" + JSON.stringify(data));
                // AAJS2.makePostRequestWithXhr(JSON.stringify(data));
            }, 300);
        }
        console.log("zh:initAd end");
    }



    initAdForPage() {
        if (sys.isNative && sys.platform === sys.Platform.ANDROID) {
            let deviceId = AAJS2.getDeviceUserId();
            console.log("zh:checkstatus:", ATRewardedVideoSDK.checkAdStatus(AAJS2.getPlacementId()));
            var setting = {};
            setting[ATRewardedVideoSDK.userIdKey] = deviceId;
            ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementId(), setting);
        }
    }




    

    /**
     * AD播放完毕的事件处理
     */
    handleCustomEvent() {
        console.log('zh:00收到发送的事件')
        let adKey = "zh_ad_type"
        let idx =sys.localStorage.getItem(adKey);//0,1,2,3 
        console.log('zh:onAdOverEve idx =' + idx)
        switch (idx) {
            case '1':
                console.log('zh:开始使用消除道具')
                GlobalManager.getInstance().callMethod('event_AddChanceRevive_util');
                break;        
            default:
                console.log('zh:未知的道具类型AD' + idx)
        }
        sys.localStorage.setItem(adKey, '');
    }




    onEnable() {
        this.addEventArr([
            msgac.levelRestart,
            msgac.lifeBack,
            msgac.adjustPlayerInCenter,
            msgac.levelNext,
            msgac.levelEspSuccess,
            msgac.skillLayerRefresh,
            msgac.judgeLevelSuccess,
            msgac.showHitEffect,
            msgac.startFight,
            msgac.fightEnd,
            msgac.skillAdd,
            msgac.LevelLayerClose,
        ]);
    }

    onDisable() {
        this.removeEventArr([
            msgac.levelRestart,
            msgac.lifeBack,
            msgac.adjustPlayerInCenter,
            msgac.levelNext,
            msgac.levelEspSuccess,
            msgac.skillLayerRefresh,
            msgac.judgeLevelSuccess,
            msgac.showHitEffect,
            msgac.startFight,
            msgac.fightEnd,
            msgac.skillAdd,
            msgac.LevelLayerClose,
        ]);
    }

    initUI() {
        this.initEffect();

        this.initSkillLayer();
        this.schedule(() => {
            let arr = this.skillLayer.children;
            for (let i in arr) {
                let node = arr[i];
                Tween.stopAllByTarget(node);
                node.setRotationFromEuler(0, 0, 0);
            }
            let pNode = utilTools.getRandomItemByArr(arr);
            cocosUtil.tweenShake(pNode, 0.1, 0, 0);
        }, 0.4 * 5, macro.REPEAT_FOREVER, 0);

        this.loadLevelLayer();
    }

    initSkillLayer() {
        let arr = designManager.instance.getTable(constants.tableName.skill);
        let tmpArr = [];
        for (let i in arr) {
            let row = arr[i];
            if (this.level % 2 == 0) {
                if (row.id == 1 || row.id == 3) {
                    tmpArr.push(row);
                }
                continue;
            }
            if (row.id == 2 || row.id == 4) {
                tmpArr.push(row);
            }
        }
        arr = tmpArr;

        this.skillLayer.getComponent(ItemLayer).initUI(this, arr, this.refreshSkillItem.bind(this));
    }

    refreshSkillItem(itemUI: Node, row: any) {
        let numNode = find("num", itemUI);
        let adNode = find("ad", itemUI);
        let iconNode = find("icon", itemUI);
        let skill = playerModel.instance.getSkillById(row.id);
        iconManager.instance.setSprite(iconNode, row.icon);
        if (skill.num > 0) {
            numNode.active = true;
            adNode.active = false;
            this.setString(numNode, "x" + skill.num);
        } else {
            numNode.active = false;
            adNode.active = true;
        }
    }

    initLocaltext() {
        this.setString(this.levelNode, this.level);
    }

    initEffect() {
        cocosUtil.tweenShake(this.btnAdAtk);
        cocosUtil.tweenShake(this.btnSkip);
    }

    loadLevelLayer() {
        this.initLocaltext();

        this.cloundLayer1.active = false;
        this.cloundLayer2.active = false;
        sdkManager.instance.sendEventLevelStart(this.level);
        levelManager.instance.loadLevel(this.level, () => { }, (err, prefab) => {
            // 关卡UI生成
            for (let i in this.levelLayer.children) {
                this.levelLayer.children[i].destroy();
            }
            let node = instantiate(prefab);
            node.parent = this.levelLayer;
            cocosUtil.setPositionY(node, 0);

            // 设置视图滚动高度
            let listContentTf = this.listContentLayer.getComponent(UITransform);
            let levelTf = node.getComponent(UITransform);
            listContentTf.height = levelTf.height;

            // 从UI中初始化关卡数据
            levelManager.instance.initLevelLayer(this.level, node, this);

            // 初始化玩家
            for (let i in this.playerLayer.children) {
                this.playerLayer.children[i].destroy();
            }
            levelManager.instance.initRolePlayer(this.playerLayer);

            this.listScrollView.scrollToBottom(1);

            // 新手引导
            this.refreshHandLayer();

            // 随机云位置
            this.initCloundLayer();

            this.initSkillLayer();
        });

        if (this.level >= levelManager.instance.shopSecretLevelMin) {
            // 弹出神秘商店
            let obj: any = {};
            obj.type = constants.shopTypes.secret;
            this.openLayer(constants.layers.ShopLayer, obj);
        }
    }

    initCloundLayer() {
        this.cloundLayer1.active = true;
        this.cloundLayer2.active = true;

        let height = this.listContentLayer.getComponent(UITransform).height;
        let gap = 200;
        let maxY = height - gap;
        let minY = gap;
        let midY = (minY + maxY) * 0.5;

        let posY = utilTools.getFloatValue(midY, maxY);
        let clound = this.cloundLayer1.children[0];
        let pos = clound.getPosition();
        pos.y = -posY;
        clound.setPosition(pos);

        posY = utilTools.getFloatValue(minY, midY);
        clound = this.cloundLayer2.children[0];
        pos = clound.getPosition();
        pos.y = -posY;
        clound.setPosition(pos);
    }

    refreshHandLayer() {
        if (this.level != 1 || playerModel.instance.getHandShow()) {
            return;
        }

        if (this.handNode) {
            this.removeHandLayer();
        }

        let floorLayer = levelManager.instance.floorLayerObj[1];
        let firstMonster = floorLayer.children[0].children[0];
        cocosUtil.addHand(firstMonster, this.node, (handNode) => {
            // 根据实际情况再进行微调
            let pos = handNode.getPosition();
            pos.x += 40;
            pos.y -= 10;
            handNode.setPosition(pos);

            handNode["attachNode"] = firstMonster;
            this.handNode = handNode;
        });

    }

    removeHandLayer() {
        this.handNode.destroy();
        this.handNode = null;
    }

    levelRestartRet(data: any) {
        this.loadLevelLayer();
    }

    lifeBackRet(data: any) {
        levelManager.instance.playerBackLife();
    }

    adjustPlayerInCenterRet(data: any) {
        let playerNode = levelManager.instance.playerRoleItem.node;
        let pos = cocosUtil.convertToWorldSpace(playerNode);
        pos = cocosUtil.convertToNodeSpace(this.playerLayer, pos);

        let maxOffset = this.listScrollView.getMaxScrollOffset();

        let playerNodeTf = playerNode.getComponent(UITransform);
        let dy = Math.abs(pos.y) - playerNodeTf.height * (1 - playerNodeTf.anchorY);
        let offsetY = dy - this.listScrollView.node.getComponent(UITransform).height * 0.5;
        if (offsetY < 0) {
            offsetY = 0;
        }
        if (offsetY > maxOffset.y) {
            offsetY = maxOffset.y;
        }
        let offset = this.listScrollView.getScrollOffset();
        if (Math.abs(offset.y - offsetY) <= 50) {
            // 微量移动位置，就不跟随了
            return;
        }
        this.listScrollView.scrollToOffset(v2(0, offsetY), 0.6);

    }

    levelNextRet(data) {
        this.level = playerModel.instance.getLevel();
        this.loadLevelLayer();
    }

    levelEspSuccessRet(data) {
        this.closeLayer();
        let obj: any = {};
        obj.openLevelEspSelectLayer = true;
        this.openLayer(constants.layers.StartLayer, obj);
    };

    skillLayerRefreshRet(data) {
        this.skillLayer.getComponent(ItemLayer).refreshUI();
    }

    judgeLevelSuccessRet(data) {
        levelManager.instance.judgeLevelSuccess();
    }

    showHitEffectRet(data: any) {
        this.hitEffectLayer.getComponent(ItemLayer).addItem(data, this.refreshEffectItem.bind(this));
    }
    refreshEffectItem(itemUI: Node, data: any) {
        let skeleton = itemUI.getComponent(sp.Skeleton);
        skeleton.setAnimation(0, "hit1", false);
        itemUI.setPosition(cocosUtil.convertToNodeSpace(itemUI, data.pos));
        skeleton.setEndListener(() => {
            this.hitEffectLayer.getComponent(ItemLayer).removeItemUIByItemUI(itemUI);
        });
    }

    startFightRet(data: any) {
        this.fightLayer.active = true;
        this.fightLayer.getComponent(FightLayer).loadFightData(data);
        if (this.handNode) {
            this.handNode.active = false;
        }
    }

    fightEndRet(data: any) {
        if (this.handNode) {
            this.handNode.active = true;
        }

        if (!data.isWin) {
            data.name = constants.layers.GameContinueLayer.name;
            data.nextLayerName = constants.layers.FightFailLayer.name;
            eventManager.instance.send(msgac.openLayer, data);
        }
    }

    skillAddRet(data: any) {
        let node = new Node();
        node.addComponent(Sprite);
        let row = designManager.instance.getRowById(constants.tableName.skill, data.skillId);
        iconManager.instance.setSprite(node, row.icon);
        node.parent = this.node;

        let pos = cocosUtil.convertToNodeSpace(node, data.pos);
        node.setPosition(pos);
        node.getComponent(UITransform).width = 100;
        node.getComponent(UITransform).height = 100;
        let itemnUI = this.skillLayer.getComponent(ItemLayer).idItemUIObj[row.id];
        let posTarget = cocosUtil.convertToWorldSpace(itemnUI);
        posTarget = cocosUtil.convertToNodeSpace(node, posTarget);

        tween(node).to(0.5, {
            position: posTarget
        }).call(() => {
            node.destroy();
            this.skillLayer.getComponent(ItemLayer).refreshItemUIById(row.id);
        }).start();
    }

    LevelLayerCloseRet(data: any) {
        this.closeLayer();
    }

    process_event(event: any) {
        let ac = event.ac;
        let data = event.data;
        switch (ac) {
            case msgac.LevelLayerClose:
                this.LevelLayerCloseRet(data);
                break;
            case msgac.skillAdd:
                this.skillAddRet(data);
                break;
            case msgac.fightEnd:
                this.fightEndRet(data);
                break;
            case msgac.startFight:
                this.startFightRet(data);
                break;
            case msgac.showHitEffect:
                this.showHitEffectRet(data);
                break;
            case msgac.judgeLevelSuccess:
                this.judgeLevelSuccessRet(data);
                break;
            case msgac.skillLayerRefresh:
                this.skillLayerRefreshRet(data);
                break;
            case msgac.levelEspSuccess:
                this.levelEspSuccessRet(data);
                break;
            case msgac.levelNext:
                this.levelNextRet(data);
                break;
            case msgac.adjustPlayerInCenter:
                this.adjustPlayerInCenterRet(data);
                break;
            case msgac.lifeBack:
                this.lifeBackRet(data);
                break;
            case msgac.levelRestart:
                this.levelRestartRet(data);
                break;
            default:
                break;
        }
    }

    onButtonClick(node: Node, name: string) {
        if (name.length > 5 && name.substring(0, 5) == "role_") {
            this.onClickRoleItem(node);
            return;
        }
        switch (name) {
            case "skillItem":
                this.onClickSkillItem(node);
                break;
            case "btnAdAtk":
                this.onClickBtnAdAtk(node);
                break;
            case "btnReset":
                this.onClickBtnReset(node);
                break;
            case "btnBack":
                this.onClickBtnBack(node);
                break;
            case "btnSkip":
                this.onClickBtnSkip(node);
                break;
            default:
                break;
        }
    }

    onClickSkillItem(node) {
        if (this["skilling"]) {
            return;
        }
        let skill = playerModel.instance.getSkillById(node.item.id);
        if (skill.num <= 0) {
            let row = designManager.instance.getRowById(constants.tableName.skill, skill.id);
            sdkManager.instance.sendEvent("观看激励视频-获取" + row.name);
            sdkManager.instance.openAd((st: number) => {
                if (st != 1) {
                    return;
                }
                playerModel.instance.addSkillNumById(skill.id);
                this.skillLayer.getComponent(ItemLayer).refreshItemUIByItem(node.item);
                sdkManager.instance.sendEvent("观看完激励视频-获取" + row.name);
            });
            return;
        }
        this["skilling"] = true;
        levelManager.instance.useSkillId(skill.id, this.skillEffectLayer, () => {
            delete this["skilling"];
        });
        this.skillLayer.getComponent(ItemLayer).refreshItemUIByItem(node.item);
    }

    onClickBtnAdAtk(node: Node) {
        sdkManager.instance.sendEvent("观看激励视频-增加10点攻击");
        sdkManager.instance.openAd((st: number) => {
            if (st != 1) {
                return;
            }
            levelManager.instance.addAtk(levelManager.instance.levelAdAtkAddVal);
            sdkManager.instance.sendEvent("观看完激励视频-增加10点攻击");
        });
    }

    onClickBtnSkip(node: Node) {
        sdkManager.instance.sendEvent("观看激励视频-跳关");
        sdkManager.instance.openAd((st: number) => {
            if (st != 1) {
                return;
            }
            levelManager.instance.levelSkip();
            sdkManager.instance.sendEvent("观看完激励视频-跳关");
        });
    }

    onClickBtnReset(node: Node) {
        this.loadLevelLayer();
        sdkManager.instance.addLevelCount();
    }

    onClickRoleItem(node: Node) {
        if (this.handNode && this.handNode["attachNode"] == node) {
            playerModel.instance.setHandShow(1);
            this.removeHandLayer();
        }
        levelManager.instance.onClickRoleItem(node);
    }

    onClickBtnBack(node: Node) {
        this.openLayer(constants.layers.StartLayer);
        this.closeLayer();
    }




    
    //#region 'ad'


    onRewardedVideoAdLoaded(placementId) {
        console.log("zh:onRewardedVideoAdLoaded", placementId, "", "")
    }

    onRewardedVideoAdFailed(placementId, errorInfo) {
        console.log("zh:onRewardedVideoAdFailed", placementId, "", errorInfo)
    }

    onRewardedVideoAdPlayStart(placementId, callbackInfo) {
        console.log("zh:onRewardedVideoAdPlayStart", placementId, callbackInfo, "")
        let deviceId = AAJS2.getDeviceUserId();
        var setting = {};
        setting[ATRewardedVideoSDK.userIdKey] = deviceId;
        ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementId(), setting);
    }

    onRewardedVideoAdPlayEnd(placementId, callbackInfo) {
        console.log("zh:onRewardedVideoAdPlayEnd", placementId, callbackInfo, "")


        console.log('zh: ad over 开始发送事件11')
        //this.node.emit("handleCustomEvent");
        this.handleCustomEvent()
        console.log('zh: ad over 开始发送事件OVER22')



    }

    onRewardedVideoAdPlayFailed(placementId, errorInfo, callbackInfo) {
        console.log("zh:onRewardedVideoAdPlayFailed", placementId, callbackInfo, errorInfo)
    }
    onRewardedVideoAdClosed(placementId, callbackInfo) {
        console.log("zh:onRewardedVideoAdClosed", placementId, callbackInfo, "")


    }

    onRewardedVideoAdPlayClicked(placementId, callbackInfo) {
        console.log("zh:onRewardedVideoAdPlayClicked", placementId, callbackInfo, "")
    }

    onReward(placementId, callbackInfo) {
        //this.state = true;
    }
    //Callbacks added v5.8.10
    onAdSourceBiddingAttempt(placementId, callbackInfo) {
        console.log("zh:onAdSourceBiddingAttempt", placementId, callbackInfo, "")
    }

    onAdSourceBiddingFilled(placementId, callbackInfo) {
        console.log("zh:onAdSourceBiddingFilled", placementId, "", "")
    }

    onAdSourceBiddingFail(placementId, errorInfo, callbackInfo) {
        console.log("zh:onAdSourceBiddingFail", placementId, callbackInfo, errorInfo)
    }

    onAdSourceAttempt(placementId, callbackInfo) {
        console.log("zh:onAdSourceAttempt", placementId, callbackInfo, "")
    }

    onAdSourceLoadFilled(placementId, callbackInfo) {
        console.log("zh:onAdSourceLoadFilled", placementId, callbackInfo, "")
    }

    onAdSourceLoadFail(placementId, errorInfo, callbackInfo) {
        console.log("zh:onAdSourceLoadFail", placementId, callbackInfo, errorInfo)
    }

    onRewardedVideoAdAgainPlayStart(placementId, callbackInfo) {
        console.log("zh:onRewardedVideoAdAgainPlayStart", placementId, callbackInfo, "")

    }

    onRewardedVideoAdAgainPlayEnd(placementId, callbackInfo) {
        console.log("zh:onRewardedVideoAdAgainPlayEnd", placementId, callbackInfo, "")
    }

    onRewardedVideoAdAgainPlayFailed(placementId, errorInfo, callbackInfo) {
        console.log("zh:onRewardedVideoAdAgainPlayFailed", placementId, callbackInfo, errorInfo)
    }

    onRewardedVideoAdAgainPlayClicked(placementId, callbackInfo) {
        console.log("zh:onRewardedVideoAdAgainPlayClicked", placementId, callbackInfo, "")
    }

    onAgainReward(placementId, callbackInfo) {
        console.log("zh:onAgainReward", placementId, callbackInfo, "")
      
    }



    //#endregion

}

