import { _decorator, BlockInputEvents, Button, Color, Director, director, EventMouse, Label, Node, UITransform, v2 } from 'cc';
import { BaseSprite } from '../../GlobalScript/CommonComponent/BaseSprite';
import { NpkImage } from '../../Tool/NPKImage';
import { BaseButton } from '../../GlobalScript/CommonComponent/BaseButton';
import { AnimationNode } from '../../GlobalScript/Animation/AnimationNode';
import { UIRoot } from '../UIRoot';
import {CloseButtonNode } from '../Common/CloseButtonNode';
import { GameRootSingleton } from '../GameRootController';
const { ccclass } = _decorator;

/// 开始游戏按钮界面
@ccclass('StartGameUINode')
export class StartGameUINode extends Node {

    /// 玩法介绍
    private pressenNode: Node;

    constructor(){
        super();

        this.addComponent(UITransform).setContentSize(1067,600);

        this.init();
    }

    /// 初始化子节点
    init(){

        this.initBackground();
        this.initTitle();
        this.initStartButton();
        this.initLabel();
        this.initPressenButton();
        this.initPressen();
    }

    /// 背景
    initBackground(){
        /// 背景节点
        const backgroundNode = new Node('StartBackgroundNode');
        this.addChild(backgroundNode);

        /// 给背景节点添加 baseSprite 组件
        const backgroundComponent = backgroundNode.addComponent( BaseSprite );
        backgroundComponent.updateSpriteFrame(NpkImage.main,24);
    }

    /// 标题动画
    initTitle(){
        let titleNode = new AnimationNode('ani/title_loop.ani');
        titleNode.setPosition(544.5,-305);
        this.addChild(titleNode);
    }

    /// 开始游戏
    initStartButton(){
        /// 按钮节点
        const startButtonNode = new Node('StartButton');
        startButtonNode.setPosition(441.5,-450);

        /// 给节点添加 button 组件
        const buttonComponent = startButtonNode.addComponent( BaseButton );
        buttonComponent.ImgPath = NpkImage.main;
        buttonComponent.NormalImgIndex = 1;
        buttonComponent.HoverImgIndex = 2;
        buttonComponent.PressImgIndex = 3;
        buttonComponent.DisableImgIndex = 4;

        startButtonNode.addComponent( Button)
        startButtonNode.on(Button.EventType.CLICK,this.startOnTouchEnd,this);

        this.addChild(startButtonNode);
    }

    /// 入场次数
    initLabel(){
        const labelNode = new Node('Label');
        labelNode.setPosition(550,-421);

        const labelComponent = labelNode.addComponent( Label );
        labelComponent.string = '∞';
        labelComponent.color = new Color('24a5cd');
        labelComponent.fontSize = 18;

        this.addChild(labelNode);
    }

    /// 玩法介绍按钮
    initPressenButton(){
        /// 按钮节点
        const pressenButtonNode = new Node('PressenButton');
        pressenButtonNode.setPosition(1020,-10);

        /// 给节点添加 button 组件
        const buttonComponent = pressenButtonNode.addComponent( BaseButton );
        buttonComponent.ImgPath = NpkImage.main;
        buttonComponent.NormalImgIndex = 9;
        buttonComponent.HoverImgIndex = 10;
        buttonComponent.PressImgIndex = 11;
        buttonComponent.DisableImgIndex = 12;

        pressenButtonNode.on(Node.EventType.MOUSE_UP,this.pressenOnTouchEnd,this);

        this.addChild(pressenButtonNode);
    }

    /// 玩法介绍
    initPressen(){
        /// 节点
        this.pressenNode = new Node('Pressen');
        this.pressenNode.active = false;
        this.addChild(this.pressenNode);

        this.pressenNode.setPosition(0,0);
        this.pressenNode.addComponent( UITransform).setContentSize(1067,600);
        this.pressenNode.getComponent(UITransform).anchorPoint = v2(0, 1);

        /// 拦截下层的点击事件
        this.pressenNode.addComponent( BlockInputEvents );

        /// 给节点添加 img
        const imgNode = new Node('PressenImage');
        imgNode.setPosition(134,-21.5);
        const imgComponent = imgNode.addComponent( BaseSprite );
        imgComponent.updateSpriteFrame(NpkImage.main,25);
        this.pressenNode.addChild(imgNode);

        /// 关闭按钮
        const closeNode = new CloseButtonNode(this.closeOnTouchEnd.bind(this));
        closeNode.setPosition(767,-10);
        imgNode.addChild(closeNode);

    }

    /// 打开玩法介绍
    pressenOnTouchEnd(event:EventMouse){
        if ( event.getButton() === EventMouse.BUTTON_LEFT ){
            this.pressenNode.active = true;
        }
    }


    closeOnTouchEnd(event:EventMouse){
        this.pressenNode.active = false;
    }

    /// 开始游戏
    startOnTouchEnd(event:Event){
        const uiroot = GameRootSingleton.getInstance().UIRoot;
        uiroot.initGameUI();

        /// 一帧结束之后销毁
        director.once(Director.EVENT_END_FRAME,()=>{
            // 销毁
            this.destroy();
        });

        console.log('开始游戏');
    }

}


