import { GameTools } from "../Tool/GameTools";
import  {GameManager}  from "./GameManager";
import {EventManager, PropEffecf} from "../Tool/EventManager";
import Vector2 = Laya.Vector2;
import { GameControl } from "./GameControl";
import {GamePool} from "../Tool/GamePool";

export  default class Ball extends Laya.Script{
    private static ins:Ball;
    private _rig:Laya.RigidBody;
    //光圈
    public Light:Laya.Image;
    public TargetArray = [];
    //是否已经移动
    private  moved = false;
    //该球是否已被标记
    public mark = false;
    //是否是连带关系
    public clear = false;
    //分数被记录
    public score:number = 0;

    constructor(){
        super();
        Ball.ins = this;
    }
    public static get Instance():Ball{
        if(Ball.ins){
            return Ball.ins
        }
        else{
            return new Ball();
        }
    }
    onAwake(){
        this.Light = this.owner.getChildByName("light") as Laya.Image;
        this.Light.visible = false;
        this.owner.on(Laya.Event.CLICK,this,this.ClickBall);
        EventManager.EventOn(PropEffecf.DOUBLE,this,this.ClickDouble);
        EventManager.EventOn(PropEffecf.NONE,this,this.ClickNone);
        EventManager.EventOn(PropEffecf.HAMMER,this,this.ClickHammer);
        let score = this.owner.getChildByName("num") as Laya.FontClip;
        this.score = Number(score.value);
    }
    
    onUpdate(){
        this.CheckLight();
        if(this.clear){
            let _rig = this.owner.getComponent(Laya.RigidBody) as Laya.RigidBody;
            let _collider = this.owner.getComponent(Laya.CircleCollider) as Laya.CircleCollider;
            _collider.isSensor = true;
            _collider.enabled = false;
            _rig.enabled = false;
            this.Move(GameManager.Instance.TargetPosition);
            this.moved = true;
        }
    }
    //判断光效开关
    CheckLight(){
        if(this.TargetArray.length>0){
            let num = 0;
            for (let i = 0; i < this.TargetArray.length; i++) {
                let ball = this.TargetArray[i] as Laya.Image;
                if(ball.destroyed){
                    num ++ ;
                }
            }
            if(num == this.TargetArray.length){
                this.Light.visible = false;
            }
            else{
                this.Light.visible = true;
            }
        }
        else{
            this.Light.visible = false;
        }
    }
    //普通模式
    ClickNone(){
        this.owner.offAll();
        this.owner.on(Laya.Event.CLICK,this,this.ClickBall);
    }
    //点击双倍道具模式
    ClickDouble(){
        this.owner.offAll();
        this.owner.on(Laya.Event.CLICK,this,this.Double);
    }
    //双倍道具效果
    Double(){
        console.log("点击到了----");
        let num = this.score * 2;
        let owner = this.owner as Laya.Image;
        let _position = new Laya.Vector2(owner.x,owner.y);

        let _rig = this.owner.getComponent(Laya.RigidBody) as Laya.RigidBody;
        let _collider = this.owner.getComponent(Laya.CircleCollider) as Laya.CircleCollider;
        _collider.enabled = false;
        _rig.enabled = false;
        this.owner.removeSelf();
        this.owner.destroy();

        EventManager.EventTrigger(PropEffecf.NONE);

        let id = GamePool.Instance.PrecossData(num);
        GameManager.Instance.ScoreProgress += num;
        GamePool.Instance.CreatPrefab(id,_position);
    }
    //点击锤子道具模式
    ClickHammer(){
        this.owner.offAll();
        this.owner.on(Laya.Event.CLICK,this,this.Hammer);
    }
    //锤子道具效果
    Hammer(){
        let num = this.score;
        let owner = this.owner as Laya.Image;
        GameManager.Instance.ScoreProgress += num;

        let _rig = this.owner.getComponent(Laya.RigidBody) as Laya.RigidBody;
        let _collider = this.owner.getComponent(Laya.CircleCollider) as Laya.CircleCollider;
        _collider.enabled = false;
        _rig.enabled = false;
        this.owner.removeSelf();
        this.owner.destroy();

        EventManager.EventTrigger(PropEffecf.NONE);
        // InGameView.Instance.EffectMode(false);
        
        
    }
    onDestroy(){
        this.owner.offAll();
        EventManager.EventOff(PropEffecf.DOUBLE,this,this.ClickDouble);
        EventManager.EventOff(PropEffecf.NONE,this,this.ClickNone);
        EventManager.EventOff(PropEffecf.HAMMER,this,this.ClickHammer);
    }
    onTriggerEnter(other: any) {
        let targetparent = other.owner;
        
        if (targetparent.name != "RightCollider" && targetparent.name != "LeftCollider" && targetparent.name != "BottomCollider") {
            let num = targetparent.getChildByName("num") as Laya.FontClip;
            let ownernum = this.owner.getChildByName("num") as Laya.FontClip;
            if (Number(num.value) == Number(ownernum.value)) {
                this.TargetArray.push(targetparent);
            }
        }
    }
    onTriggerStay(other: any) {
        let targetparent = other.owner;
        if (targetparent.name != "RightCollider" && targetparent.name != "LeftCollider" && targetparent.name != "BottomCollider") {
            let num = targetparent.getChildByName("num") as Laya.FontClip;
            let ownernum = this.owner.getChildByName("num") as Laya.FontClip;
            if (Number(num.value) == Number(ownernum.value)) {
                this.TargetArray.push(targetparent);
            }
        }
    }
    onTriggerExit(other:any){
        let targetparent = other.owner;
        // if(targetparent.destroyed){
        //     for (let i = this.TargetArray.length-1; i > -1; i--) {
        //         if(this.TargetArray[i] == targetparent){
        //             this.TargetArray.splice(i,1);
        //         }
        //     }
        // }
        // else{
            if(targetparent.name != "RightCollider" && targetparent.name != "LeftCollider" && targetparent.name != "BottomCollider"){
                for (let i = this.TargetArray.length-1; i > -1; i--) {
                    if(this.TargetArray[i] == targetparent){
                        this.TargetArray.splice(i,1);
                    }
                }
            }
        // }
        
    }
    //往目标点移动
    Move(_position: Laya.Vector2) {
        if(!this.moved){
            let owner = this.owner as Laya.Image;
            let move = Laya.Tween.to(this.owner, {
                x: _position.x, y: _position.y, update: new Laya.Handler(this, function(){
    
                })
            }, 200, Laya.Ease.linearInOut, Laya.Handler.create(this, () => {
                move.clear();
                this.owner.removeSelf();
                this.owner.destroy();
                if(!GamePool.Instance.Cloned){
                    
                    let num = GamePool.Instance.PrecossData(GameManager.Instance.ScoreRecord);
                    let score = GamePool.Instance.ProcessScore(GameManager.Instance.ScoreRecord);
                    let ball = GamePool.Instance.CreatPrefab(num,GameManager.Instance.TargetPosition);
                    GameManager.Instance.ScoreProgress += score;
                    GamePool.Instance.FallDown();
                }
            }));
        }
    }
   //点击到球
    ClickBall(){
       
        //将数组中被删除掉的球移除
        for (let j = this.TargetArray.length-1; j > -1; j--) {
            if(this.TargetArray[j].destroyed){
                this.TargetArray.splice(j,1);
            }
        }
        for (let i = 0; i < this.TargetArray.length; i++) {
            let num = this.TargetArray[i].getChildByName("num") as Laya.FontClip;
        }
        let owner = this.owner as Laya.Image;
        GamePool.Instance.Cloned = false;
        GameManager.Instance.ScoreRecord = 0;
        GameManager.Instance.TargetPosition =new Vector2(owner.x,owner.y);
        this.DisPose(owner); 
    }
    //处理被点到的球的子物体
    DisPose(_node:Laya.Image){
        let ball = _node.getComponent(Ball) as Ball;
        //被点击的球的周围数量
        if(ball.TargetArray.length>0){
            //被点击的球的分数
            if(!ball.mark){
                let ownerscore = ball.score;
                GameManager.Instance.ScoreRecord += ownerscore;
                ball.mark = true;
            }
            ball.clear = true;
            
            for (let i = 0; i < ball.TargetArray.length; i++) {
                if(ball.TargetArray[i].getComponent(Ball).mark)continue;
                //记录分数
                let score = ball.TargetArray[i].getChildByName("num") as Laya.FontClip;
                GameManager.Instance.ScoreRecord += Number(score.value);
                ball.TargetArray[i].getComponent(Ball).mark = true;
                ball.TargetArray[i].getComponent(Ball).clear = true;
                
                let target = ball.TargetArray[i];
                let ball2 = target.getComponent(Ball) as Ball;
                //被点击的球的周围球的子物体数量
                if(ball2.TargetArray.length>0){
                    for (let j = 0; j < ball2.TargetArray.length; j++) {
                        if(ball2.TargetArray[j] == _node ){
                            continue
                        }
                        ball2.clear = true;
                        ball2.mark = true;
                        this.DisPose(ball2.TargetArray[j]);
                    }
                }
            }
        }
    }
    
}