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

//Author:ZCG
//CreatTime:12/26/2017
/// <summary>
/// 水炮
/// </summary>
public class WaterCannon : BaseTransformChange
{
    public Transform head;//炮头
    //字段
    public float RecordPingPongSpeed { get; private set; }
    public float RecordWaterSpeed { get; private set; }
    public bool RecordOpenPingPong { get; private set; }
    public bool RecordPutOutFire { get; private set; }

    protected override void ReplayEvent(IMessage obj)
    {
        var eventData = (EventData)obj.Data;
        if (eventData.eventType == RecordEventType.WaterSprayU)
        {
            RecordSprayerRotation data = JsonUtility.FromJson<RecordSprayerRotation>(eventData.json);
            if (data.gameObjID == GetComponent<BaseGameObjInfo>().gameObjID)
            {
                head.transform.localEulerAngles = data.rotation;
            }
        }
        else if (eventData.eventType == RecordEventType.WaterSprayA)
        {
            RecordSprayerRotation data = JsonUtility.FromJson<RecordSprayerRotation>(eventData.json);
            if (data.gameObjID == GetComponent<BaseGameObjInfo>().gameObjID)
            {
                base.obj.transform.localEulerAngles = data.rotation;
            }
        }   
        else if(eventData.eventType == RecordEventType.WaterSprayPlus)
        {
            if(eventData.json == gameObject.name)
            {
                waterControl.Increase(0.5f);
            }
        }
        else if(eventData.eventType ==RecordEventType.WaterSprayMinus)
        {
            if (eventData.json == gameObject.name)
            {
                waterControl.Decrease(0.5f);
            }
        }
    }
    
    protected virtual void Init()
    {
        y = true;
        transformType = TransformType.Rotation;
        valueChanage = ValueChange.PingPong;
        min = -90;
        max = 90;
    }  

    private void setSkill(IMessage obj)
    {
        var gameObjID = GetComponent<BaseGameObjInfo>().GameObjID;
        if((long)obj.Data == gameObjID)
        {
            PutOutFire();  
        }
    }
    protected override void PressureUp(IMessage obj)
    {
        //出水变大
        var gameObjID = (long)obj.Sender;
        if (gameObjID == GetComponent<BaseGameObjInfo>().GameObjID)
        {
            waterControl.Increase(0.5f);
            AddRecordEvent(RecordEventType.WaterSprayPlus);
        }
    }
    protected override void PressureDown(IMessage obj)
    {
        //出水变小
        var gameObjID = (long)obj.Sender;
        if (gameObjID == GetComponent<BaseGameObjInfo>().GameObjID)
        {
            waterControl.Decrease(0.5f);
            AddRecordEvent(RecordEventType.WaterSprayMinus);
        }
    }
    protected override void U(IMessage obj)
    {
        var gameObjID = (long)obj.Sender;
        if (gameObjID == GetComponent<BaseGameObjInfo>().GameObjID)
        {
            if (head.localRotation.x < -0.1)
            {
                head.Rotate(Vector3.right * Time.deltaTime * 100);               
                AddRecordUJEvent();
            }    
        }
    }
    protected override void J(IMessage obj)
    {
        var gameObjID = (long)obj.Sender;
        if (gameObjID == GetComponent<BaseGameObjInfo>().GameObjID)
        {
            if (head.localRotation.x > -0.9)
            {
                head.Rotate(Vector3.right * Time.deltaTime * -100);                
                AddRecordUJEvent();
            }             
        }
    }
    protected override void A(IMessage obj)
    {
        var gameObjID = (long)obj.Sender;
        if (gameObjID == GetComponent<BaseGameObjInfo>().GameObjID)
        {
            if (base.obj.localRotation.y > -0.8)
            {
                base.obj.Rotate(Vector3.up * Time.deltaTime * -100);                
                AddRecordADEvent();
            }                
        }
    }
    protected override void D(IMessage obj)
    {
        var gameObjID = (long)obj.Sender;
        if (gameObjID == GetComponent<BaseGameObjInfo>().GameObjID)
        {
            if (base.obj.localRotation.y < 0.8)
            {
                base.obj.Rotate(Vector3.up * Time.deltaTime * 100);               
                AddRecordADEvent();
            }                
        }
    }   
    private void AddRecordUJEvent()
    {
        if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal)
        {
            var eventData = new EventData();
            eventData.time = RecordManager.Instance.RecordTimer;
            eventData.cloneObjType = GetComponent<BaseGameObjInfo>().gameObjType;
            eventData.eventType = RecordEventType.WaterSprayU;
            RecordSprayerRotation data = new RecordSprayerRotation();
            data.gameObjID = GetComponent<BaseGameObjInfo>().gameObjID;
            data.rotation = head.transform.localEulerAngles;
            eventData.json = JsonUtility.ToJson(data);
            RecordManager.Instance.jsonData.eventDataList.Add(eventData);
        }
    }
    private void AddRecordADEvent()
    {
        if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal)
        {
            var eventData = new EventData();
            eventData.time = RecordManager.Instance.RecordTimer;
            eventData.cloneObjType = GetComponent<BaseGameObjInfo>().gameObjType;
            eventData.eventType = RecordEventType.WaterSprayA;
            RecordSprayerRotation data = new RecordSprayerRotation();
            data.gameObjID = GetComponent<BaseGameObjInfo>().gameObjID;
            data.rotation = obj.transform.localEulerAngles;
            eventData.json = JsonUtility.ToJson(data);
            RecordManager.Instance.jsonData.eventDataList.Add(eventData);
        }
    }
    public ParticleControlOfType waterControl;
    protected override void Awake()
    {
        obj = transform.Find("Rotate");
        head = obj.Find("Head");
        Init();
        waterControl = obj.Find("Head/WaterStraight/WaterStraightPos/WaterStraight").GetComponentInChildren<ParticleControlOfType>();
        waterControl.gameObject.SetActive(false);
        base.Awake();
    }
    public float GetWaterScale()
    {
        return waterControl.GetScaleValue();
    }
    public void SetWaterScale(float value)
    {
        waterControl.SetScaleValue(value);
    }
    protected override void Start()
    {
        RecordPutOutFire = openWater;
        RecordOpenPingPong = openPingPong;
        RecordPingPongSpeed = speed;
        RecordWaterSpeed = waterControl.GetScaleValue();
    }
    /// <summary>
    /// 是否打开水炮
    /// </summary>
    private bool openWater;
    /// <summary>
    /// 是否开启摇摆
    private bool openPingPong;
 
    protected override void UpdateTransform()
    {
        if (openPingPong)
            base.UpdateTransform();       
    }
    public void SetOpenPingPong(bool value)
    {
        openPingPong = value;
    }
    public void SetOpenWater(bool value)
    {
        openWater = value;
        if (openWater)
            OpenWaterCannon();
        else
            CloseWaterCannon();
    }
    /// <summary>
    /// 打开水炮
    /// </summary>
    public void OpenWaterCannon()
    {
        waterControl.gameObject.SetActive(true);        
    }
    /// <summary>
    /// 关闭水炮
    /// </summary>
    public void CloseWaterCannon()
    {
        waterControl.gameObject.SetActive(false);
        //GetComponent<ObjDrag>().enabled = true;
    }
    /// <summary>
    /// 获取摇摆速度
    /// </summary>
    public float GetPingPongSpeed()
    {
        return speed;
    }
    /// <summary>
    /// 设置射水速度
    /// </summary>
    public void SetWaterSpeed(float value)
    {
        waterControl.SetScaleValue(value);
    }
    /// <summary>
    /// 设置摇摆速度
    /// </summary>
    /// <param name="Value"></param>
    public void SetPingPongSpeed(float value)
    {
        speed = value;
    }
    /// <summary>
    /// 开启摇摆
    /// </summary>
    /// <param name="isOn"></param>
    public void OpenPingPong()
    {
        openPingPong = !openPingPong;
    }
    /// <summary>
    /// 打开关闭水炮
    /// </summary>
    /// <param name="isOn"></param>
    public void PutOutFire()
    {
        openWater = !openWater;
        if (openWater)
            OpenWaterCannon();
        else
            CloseWaterCannon();
    }    
    public void Confirm()
    {
        RecordPingPongSpeed = speed;
        RecordWaterSpeed = waterControl.GetScaleValue();
        RecordOpenPingPong = openPingPong;
        RecordPutOutFire = openWater;
    }
    public void Revocation()
    {
        SetWaterSpeed(RecordWaterSpeed);
        SetPingPongSpeed(RecordPingPongSpeed);
        OpenWaterCannon();        
    }
    /// <summary>
    /// 默认初始化
    /// </summary>
    public void PutOutFireInitialize()
    {
        openWater = true;
        openPingPong = true;
        Confirm();
    }
}
public class RecordSprayerRotation
{
    public long gameObjID;
    public Vector3 rotation;
}