import MiniMapItem from "./MiniMapItem";
import Stage from "../../Stage";
import { __SceneData } from "../../../../data/sdata/SManage";

/**
 * 场景地图
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class MiniMap extends cc.Component {

    @property(cc.Node)
    touchLayer: cc.Node = null;

    @property(cc.Node)
    mapLayer: cc.Node = null;

    @property(cc.Node)
    mStageNode: cc.Node = null;

    public stage:Stage;
    public sceneData:__SceneData;

    public callback:(info:MiniMapItem)=>void;

    onLoad(){
        let main = this.stage.main;
        let stageData = main.player.stage;
        let index = 0;
        let nodes = this.mStageNode.children;
        for (let i = 0; i < nodes.length; i++) {
            const node = nodes[i];
            let item = node.getComponent(MiniMapItem);
            if(item){
                item.stage = this.stage;
                item._stageData = this.sceneData.list[index++];
                item.setCallback((mmItem:MiniMapItem)=>{
                    this.onclickItem(mmItem);
                });
                if(stageData.stageIndex >= item._stageData.id - 1){
                    node.active = true
                }else{
                    node.active = false
                }
            }
        }
    }
    start(){
        if(this.stage.inGuide == 1){
            this.stage.inGuide  = 2
            this.openGuide()
        }
    }

    private flushOpen(){
        let main = this.stage.main;
        let stageData = main.player.stage;
        let nodes = this.mStageNode.children;
        for (let i = 0; i < nodes.length; i++) {
            const node = nodes[i];
            let item = node.getComponent(MiniMapItem);
            if(item){
                if(stageData.stageIndex >= item._stageData.id - 1){
                    node.active = true
                }else{
                    node.active = false
                }
            }
        }
        
    }

    public onEnable(){
        this.flushOpen()
        if(this.stage.inGuide != 1){
            let winSize = cc.winSize;
            let touch_move_count = 0;
            this.touchLayer.on(cc.Node.EventType.TOUCH_START, (event: any) => {
                touch_move_count = 0;
                this.touchLayer['_touchListener'].setSwallowTouches(false);
            });
            this.touchLayer.on(cc.Node.EventType.TOUCH_MOVE, (event: any) => {
                touch_move_count ++;
                if(touch_move_count > 5){
                    this.touchLayer['_touchListener'].setSwallowTouches(true);
                }
                let delta = event.getDelta();
                this.mapLayer.x += delta.x;
                this.mapLayer.y += delta.y;
                if (this.mapLayer.x < winSize.width - this.mapLayer.width) {
                    this.mapLayer.x = winSize.width - this.mapLayer.width;
                }
                if (this.mapLayer.x > 0) {
                    this.mapLayer.x = 0;
                }
                if (this.mapLayer.y <  winSize.height - this.mapLayer.height) {
                    this.mapLayer.y =  winSize.height - this.mapLayer.height;
                }
                if (this.mapLayer.y > 0) {
                    this.mapLayer.y = 0;
                }
            });
            // this.touchLayer.on(cc.Node.EventType.TOUCH_END, (event: any) => {
            //     this.touchLayer['_touchListener'].setSwallowTouches(false);
            // });
            this.touchLayer.on(cc.Node.EventType.TOUCH_CANCEL, (event: any) => {
                this.touchLayer['_touchListener'].setSwallowTouches(false);
            });
        }
    }
    public onDisable(){
        this.touchLayer.off(cc.Node.EventType.TOUCH_START);
        this.touchLayer.off(cc.Node.EventType.TOUCH_MOVE);
        this.touchLayer.off(cc.Node.EventType.TOUCH_END);
        this.touchLayer.off(cc.Node.EventType.TOUCH_CANCEL);
    }
    public onclickItem(item:MiniMapItem){
        if(this.callback){
            this.callback(item);
            if(this.clickCallback){
                this.clickCallback()
            }
        }
    }
    public setCallback(callback:(info:MiniMapItem)=>void){
        this.callback = callback;
    }
    /**
     * 点击回调
     */
    private clickCallback:()=>void;
    private setClickCallback(clickCallback:()=>void){
        this.clickCallback = clickCallback
    }
    /**
     * 打开引导
     */
    public openGuide(){
        let main = this.stage.main
        let guideMask = main.mGuideMask
        guideMask.setTargetNode(this.mStageNode.children[0])
        guideMask.show()

        this.setClickCallback(()=>{
            guideMask.close()
        })
    }


}
