import { GTime } from '../../libs/tc/util/GTime';
import { Data } from './../../libs/tw/data/Data';
import { GetRoleData } from './../../libs/tw/data/common/getRole/GetRoleData';
import { NetManager } from '../../libs/tw/manager/NetManager';
import { TwLang } from './../../libs/tw/util/TwLang';
import { GFun } from './../../libs/tc/util/GFun';
import { DataManager } from '../../libs/tw/manager/DataManager';
import SceneTransitionAni from '../SceneTransitionAni';
import layers from "../../src/views/layers";
import Scene from '../views/Scene';
import SceneCtrl from '../ctrls/sceneCtrl';
import { ModuleTypes } from '../types/sceneTypes';
export default class GameOverScene extends Scene {
    protected get skinKey() { return 'GameOver' }

    constructor(data) {
        super();
        // this.initUI(data);
    }

    public bg: eui.Image;
    public gameInfoBg: eui.Image;
    public dogAllScoreBg: eui.Image;
    public catAllScoreBg: eui.Image;
    public vsCircle: eui.Image;
    public vsCenter: eui.Image;
    public shareBtn: eui.Button;
    public againBtn: eui.Button;
    public dogScoreTxt: eui.Label;
    public catScoreTxt: eui.Label;
    public gameInfoTxt: eui.Label;
    public lotteryCountTimeTxt: eui.Label;
    public infoTips: eui.Label;
    public shareTips: eui.Label;
    public shareGuideDark: eui.Rect;
    public shareGuideTitle: eui.Image;



    public camp: string;
    private score: number = 0;
    private dogScore: number = 0;
    private catScore: number = 0;
    private endTime: number = 0;
    private timer: egret.Timer;

    start(data) {
        this.camp = data.camp;
        this.score = data.score;
        console.log(this.camp, DataManager.ins.getData('submitScore'))

        this.initTransitionAni();

        this.initScene();

    }

    initTransitionAni() {
        let nBg = new eui.Image();
        nBg.source = `startBg_png`;
        if (this.camp == 'cat') {
            nBg.x = -750
        } else {
            nBg.x = 0;
        }
        layers.sceneLayer.addChildAt(nBg, 0);

        SceneTransitionAni.open(() => {
            layers.sceneLayer.removeChild(nBg);
        }, this);
    }

    initScene() {
        if (this.camp == 'cat') {
            this.bg.x = -750
            this.gameInfoBg.source = `catScoreBg_png`
        } else {
            this.bg.x = 0;
            this.gameInfoBg.source = `dogScoreBg_png`
        }

        this.gameInfoTxt.text = `为你方赢得${this.score}分`;
        let dogData = { score: this.dogScore };
        let catData = { score: this.catScore };
        let targetDogScore = DataManager.ins.getData('submitScore') && DataManager.ins.getData('submitScore').data && DataManager.ins.getData('submitScore').data.json && DataManager.ins.getData('submitScore').data.json.dogScore ? Number(DataManager.ins.getData('submitScore').data.json.dogScore) : 0;
        let targetCatScore = DataManager.ins.getData('submitScore') && DataManager.ins.getData('submitScore').data && DataManager.ins.getData('submitScore').data.json && DataManager.ins.getData('submitScore').data.json.catScore ? Number(DataManager.ins.getData('submitScore').data.json.catScore) : 0;


        if (targetCatScore > targetDogScore) {
            if (this.camp == 'cat') {
                this.shareTips.text = `快拉更多的伙伴来加入战队吧`
            } else {
                this.shareTips.text = `你所在的阵营处于劣势\n快拉更多的伙伴来战斗哇`
            }
        } else {
            if (this.camp == 'dog') {
                this.shareTips.text = `快拉更多的伙伴来加入战队吧`
            } else {
                this.shareTips.text = `你所在的阵营处于劣势\n快拉更多的伙伴来战斗哇`
            }
        }

        let dogScoreTW = egret.Tween.get(dogData, {
            onChange: () => {
                this.dogScoreTxt.text = Math.floor(dogData.score) + '';
            }
        });
        dogScoreTW.to({ score: targetDogScore }, 1000);
        let catScoreTW = egret.Tween.get(catData, {
            onChange: () => {
                this.catScoreTxt.text = Math.floor(catData.score) + '';
            }
        });
        catScoreTW.to({ score: targetCatScore }, 1000);

        this.endTime = DataManager.ins.getData('getValentineInfo') && DataManager.ins.getData('getValentineInfo').data && DataManager.ins.getData('getValentineInfo').data.awardTime ? DataManager.ins.getData('getValentineInfo').data.awardTime : 1550145600;
        console.log(this.endTime, Math.floor(Date.now() / 1000));

        let leftTime = this.endTime - Math.floor(Date.now() / 1000);
        if (GTime.getCountDown(leftTime, 'dhms')[0]) {
            this.lotteryCountTimeTxt.text = `距离本次对抗结束还剩${GTime.getCountDown(leftTime, 'dhms')[0]}天${GTime.getCountDown(leftTime, 'dhms')[1]}小时${GTime.getCountDown(leftTime, 'dhms')[2]}分钟`;
        } else if (GTime.getCountDown(leftTime, 'dhms')[1]) {
            this.lotteryCountTimeTxt.text = `距离本次对抗结束还剩${GTime.getCountDown(leftTime, 'dhms')[1]}小时${GTime.getCountDown(leftTime, 'dhms')[2]}分钟`;
        } else if (GTime.getCountDown(leftTime, 'dhms')[2]) {
            this.lotteryCountTimeTxt.text = `距离本次对抗结束还剩${GTime.getCountDown(leftTime, 'dhms')[2]}分钟`;
        } else {
            this.lotteryCountTimeTxt.text = `等待开奖`;
        }

        this.timer = new egret.Timer(60000);
        //注册事件监听
        this.timer.addEventListener(egret.TimerEvent.TIMER, this.timerFunc, this);//计时中
        this.timer.start();
    }

    timerFunc() {
        console.log('更新时间')
        let leftTime = this.endTime - Math.floor(Date.now() / 1000);
        if (GTime.getCountDown(leftTime, 'dhms')[0]) {
            this.lotteryCountTimeTxt.text = `距离本次对抗结束还剩${GTime.getCountDown(leftTime, 'dhms')[0]}天${GTime.getCountDown(leftTime, 'dhms')[1]}小时${GTime.getCountDown(leftTime, 'dhms')[2]}分钟`;
        } else if (GTime.getCountDown(leftTime, 'dhms')[1]) {
            this.lotteryCountTimeTxt.text = `距离本次对抗结束还剩${GTime.getCountDown(leftTime, 'dhms')[1]}小时${GTime.getCountDown(leftTime, 'dhms')[2]}分钟`;
        } else if (GTime.getCountDown(leftTime, 'dhms')[2]) {
            this.lotteryCountTimeTxt.text = `距离本次对抗结束还剩${GTime.getCountDown(leftTime, 'dhms')[2]}分钟`;
        } else {
            this.lotteryCountTimeTxt.text = `等待开奖`;
        }
    }

    initEvents() {
        this.againBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_againBtn, this);
        this.shareBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_shareBtn, this);
        this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
        this.shareGuideDark.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_shareDarkBtn, this);
    }

    removeEvents() {
        this.againBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_againBtn, this);
        this.shareBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_shareBtn, this);
        this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
        this.shareGuideDark.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_shareDarkBtn, this);
    }

    onTap_againBtn() {
        NetManager.ins.getInfo(() => {
            SceneCtrl.instance.change(ModuleTypes.START_SCENE);
        });
    }

    onTap_shareBtn() {
        this.shareGuideDark.visible = true;
        this.shareGuideTitle.visible = true;
        this.addChild(this.shareGuideDark);
        this.addChild(this.shareGuideTitle);
    }

    onTap_shareDarkBtn() {
        this.shareGuideDark.visible = false;
        this.shareGuideTitle.visible = false;
    }

    onEnterFrame() {
        this.vsCircle.rotation += 1;
    }

}