
import { DispatcherManager } from "../Game/DispatcherManager";
import { ui } from "../ui/layaMaxUI";
import {DisplayerCarController} from "../Net/DisplayerCarController";
import {GuangGao} from "../Net/GuangGao";
import  {ViewManager, ViewType } from "../Net/ViewManager";
import MainView from "./MainView";
import {MateManager} from "../Manager/MateManager";
import { WXSdk } from "../Net/WXSdk";
import { FYHD_Sdk } from "../Net/FYHD_Sdk";
import { GameManager } from "../Game/GameManager";

export default class TrialView extends ui.TrialViewUI implements SC {

    private banner:any;
    constructor() {
        super();
        this.Resize();
        DispatcherManager.on(Laya.Event.RESIZE, this, this.Resize);
    }
    OnShow(data?: any) {
        this.RegButton();
        this.RefreshData();
        this.RegButton();
        this.RefreshUI();
        if (GameManager.GameCount < 1 || GameManager.GameCount >= 3){
            console.log("第一局游戏》》》》");
            GuangGao.Instance.showBanner2((_banner)=>{
                if(_banner){
                    this.banner = _banner["banner"];
                }
            })
            this.backBtn.y = Laya.stage.height-this.backBtn.height-300;
        }
        else{
            console.log("非第一局游戏1》》》》");
            if(FYHD_Sdk.iswhite){
                this.backBtn.y = Laya.stage.height-this.backBtn.height-50;
            }
            else{
                GuangGao.Instance.showBanner2();
                this.backBtn.y = Laya.stage.height-this.backBtn.height-300;
            }
        }

    }
    RefreshUI() {
        DisplayerCarController.Instance.InitDis(this.viewPort);
    }
    RefreshData() {
    }
    //点击不试用按钮次数
    clicknum = 0;
    RegButton() {
        this.ExitBtn.visible = FYHD_Sdk.iswhite;
        this.ExitBtn.clickHandler = new Laya.Handler(this,this.ClickExit);
        this.videoBtn.CZ = () => {
            this.VideoHandler();
        };
        this.backBtn.CZ = () => {
            this.clicknum ++;
            if (GameManager.GameCount < 1 || GameManager.GameCount >= 3) {
                console.log("第一局游戏2》》》》");
                DisplayerCarController.Instance.Reset();
                this.BackUi();
            }
            else {
                console.log("非第一局游戏2》》》》");
                if (FYHD_Sdk.iswhite) {
                    if (this.clicknum <= 1) {
                        GuangGao.CheatBanner2(this.backBtn);
                    }
                    else {
                        DisplayerCarController.Instance.Reset();
                        this.BackUi();
                    }
                }
                else {
                    DisplayerCarController.Instance.Reset();
                    this.BackUi();
                }
            }
    }
    }
    ClickExit(){
        ViewManager.Instance.AddView(ViewType.ExitView);
    }
    VideoHandler() {

        GuangGao.ShowVideo((isok) => {
            if (isok) {
                WXSdk.AldEvent("看视屏试用");
                this.VideoCom();
            }
        });
    }

    VideoCom() {
        DisplayerCarController.Instance.Reset();
        this.BackUi();
        MateManager.Instance.Trial();
    }
    BackUi() {
        ViewManager.Instance.CloseView(ViewType.TrialView);
        if(MainView.Instance)MainView.Instance.TryClick();
    }
    OnClose() {
        DispatcherManager.off(Laya.Event.RESIZE, this, this.Resize);
        // GuangGao.Clear();
        if(FYHD_Sdk.iswhite){
            if(GuangGao.Instance._bannerAd2){
                GuangGao.Instance._bannerAd2.hide();
            }
            if(GuangGao.Instance._bannerAd3){
                GuangGao.Instance._bannerAd3.hide();
            }
        }
        else{
            GuangGao.Instance.hideBanner2();
        }
    }

    Resize() {
        this.height = Laya.stage.height;
    }
}