using AX.MessageSystem;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WaterCannonAttribute : ToolAttribute 
{
    public Transform obj;//需要控制的物体
    private Transform head;//炮头
    public ParticleControlOfType waterControl; //水花
    public WaterReceiver receiver;
    public WaterCannon control;
    public bool IsAutoSwing;
    public Transform SwingTrans;
    private Vector3 orirotate;
    private float timer;
    protected override void Awake()
    {
        obj = transform.Find("Rotate");
        head = obj.Find("Head");        
        waterControl = obj.GetComponentInChildren<ParticleControlOfType>();
        control = gameObject.GetComponent<WaterCannon>();
        receiver = GetComponent<WaterReceiver>();
        waterControl.gameObject.SetActive(false);
        gameObjType = CloneObjType.WaterCannon;
        typeName = "水炮";            
        MessageDispatcher.AddListener("WaterSourceChanged", WaterSourceChanged);
        MessageDispatcher.AddListener("WaterSeparaterChange", WaterseparadChange);
        MessageDispatcher.AddListener("AutoSwing", AutoSwingCon);
        base.Awake();        
    }
    protected override void OnDestroy()
    {
        base.OnDestroy();
        MessageDispatcher.RemoveListener("WaterSourceChanged", WaterSourceChanged);
        MessageDispatcher.RemoveListener("WaterSeparaterChange", WaterseparadChange);
        MessageDispatcher.RemoveListener("AutoSwing", AutoSwingCon);
    }
    private void AutoSwingCon(IMessage obj)
    {

        if ((long)obj.Data == GetComponent<BaseGameObjInfo>().gameObjID)
        {
            IsAutoSwing = !IsAutoSwing;
            AddExecuteEvent();
        }
    }
    private void AddExecuteEvent()
    {
        if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal)
        {
            var eventData = new EventData();
            var msg = GetComponent<CloneGameObjInfo>();
            eventData.time = RecordManager.Instance.RecordTimer;
            eventData.cloneObjType = msg.gameObjType;
            eventData.eventType = RecordEventType.AutoSwing;
            var data = new RecordWaterReceiver();
            data.IsAutoSwing = IsAutoSwing;
            data.ID = GetComponent<BaseGameObjInfo>().gameObjID;
            string json = JsonUtility.ToJson(data);
            eventData.json = json;
            RecordManager.Instance.jsonData.eventDataList.Add(eventData);
        }
    }
    protected override void ExecuteEvent(IMessage obj)
    {
        base.ExecuteEvent(obj);
        var eventData = (EventData)obj.Data;
        if (eventData.eventType == RecordEventType.AutoSwing && eventData.cloneObjType == gameObjType)
        {
            var arg = JsonUtility.FromJson<RecordWaterReceiver>(eventData.json);
            if (arg.ID == GetComponent<BaseGameObjInfo>().gameObjID)
            {
                IsAutoSwing = arg.IsAutoSwing;
            }
        }

    }
    private void WaterseparadChange(IMessage obj)
    {
        var data = (Waterseparadata)obj.Data;
        var msg = GetComponent<CloneGameObjInfo>();
        if (msg.gameObjID == data.receiveId)
        {
            receiver.hasSupplier = data.IsContent;
            if (msg.gameObjID == data.receiveId && data.IsContent == false && task == "灭火")
            {

                task = "待命";
                Execute(task);
            }
        }
    }
    protected override void Execute(string TaskName)
    {
        task = TaskName;
        if (TaskName.Contains("灭火"))
        {            
            if (receiver.hasSupplier == true)
            {
               // WaterType type = GetSupplerWaterType(receiver);
                //if (type!=WaterType.none)
                {
                     control.OpenWaterCannon();
                }
            }
            else
            {
                ResourceLoadWindow.Instance.LoadTextHintWindow("请先连接水源", 2);
                task = "待命";
            }
        }        
        else
        {            
            control.CloseWaterCannon();
            control.SetOpenPingPong(false);
        }
        if (TaskName.Contains("自动摇摆"))
        {
            IsAutoSwing = true;
        }
        else
        {
            IsAutoSwing = false;
            SwingTrans.localEulerAngles = orirotate;
        }
    }
    private WaterType GetSupplerWaterType(WaterReceiver receiver)
    {
        WaterType type = WaterType.none;
        GameObject waterline = EntitiesManager.Instance.GetEntityByID(receiver.waterlinelist[0]);
        GameObject suppler = EntitiesManager.Instance.GetEntityByID(waterline.GetComponent<WaterLineInfo>().SupplierID);
        type = suppler.GetComponent<WaterSupplier>().WaterReceiverType;
        return type;
    }
    private void WaterSourceChanged(IMessage obj)
    {
        var data = (WaterConnectionData)obj.Data;
        var msg = GetComponent<CloneGameObjInfo>();
        if (msg.gameObjID == data.receiverID && data.connected == false && task == "灭火")
        {
            task = "待命";
            Execute(task);
        }
    }
    private void Update()
    {
        if (IsAutoSwing)
        {
            timer += Time.deltaTime;
            if (timer > 12f)
            {
                timer = 0f;
            }
            else if (timer < 3)
            {
                SwingTrans.localEulerAngles += new Vector3(0, Time.deltaTime * 10, 0);
            }
            else if (timer < 6)
            {
                SwingTrans.localEulerAngles += new Vector3(0, -Time.deltaTime * 10, 0);
            }
            else if (timer < 9)
            {
                SwingTrans.localEulerAngles += new Vector3(0, -Time.deltaTime * 10, 0);
            }
            else if (timer < 12)
            {
                SwingTrans.localEulerAngles += new Vector3(0, Time.deltaTime * 10, 0);
            }
        }
    }
}