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

public class LadderTruckArm : ControlTruckArm
{
    private Transform part3;
    private Transform part4;
    private Transform part5;
    private Transform part6;
    private Transform part7;
    private Transform part8;
    private Transform part9;
    public bool firstZero = true;
    public bool firstFull = false;
    public bool secondZero = true;
    public bool secondFull = false;
    public bool thirdZero = true;
    public bool thirdFull = false;
    public bool fourthZero = true;
    public bool fourthFull = false;
    public bool fifthZero = true;
    public bool fifthFull = false;
    private bool allzero = true;//下降极限
    private bool allfull = false;//上升极限
    const float Maxlength = 15.57183f;
    const float Minlength = 0.01634139f;
    private bool selfctrl = true;
    private void Start()
    {
        GameObjID = GetComponent<BaseGameObjInfo>().GameObjID;
    }
    protected override void InitObject()
    {
        HorizontalBaseSupport = this.transform.Find("Bi");
        VerticalBaseSupport = this.transform.Find("Bi/Bi1");
        part3 = this.transform.Find("Bi/Bi1/Bi2");
        part4 = this.transform.Find("Bi/Bi1/Bi2/Bi3");
        part5 = this.transform.Find("Bi/Bi1/Bi2/Bi3/Bi4");
        part6 = this.transform.Find("Bi/Bi1/Bi2/Bi3/Bi4/Bi5");
        part7 = this.transform.Find("Bi/Bi1/Bi2/Bi3/Bi4/Bi5/Bi6");
        LastArm = this.transform.Find("Bi/Bi1/Bi2/Bi3/Bi4/Bi5/Bi6/pingtai");
        part9 = this.transform.Find("Bi/Bi1/Bi2/Bi3/Bi4/Bi5/Bi6/pingtai/Sprayer");
        Part1MinFrame_y = HorizontalBaseSupport.localEulerAngles.y;
        Part2MinFrame_x = VerticalBaseSupport.localEulerAngles.x;
        Part3MinFrame_z = part3.localPosition.z;
        Part4MinFrame_z = part4.localPosition.z;
        Part5MinFrame_z = part5.localPosition.z;
        Part6MinFrame_z = part6.localPosition.z;
        Part7MinFrame_z = part7.localPosition.z;
    }
    public bool AllZero
    {
        get
        {
            return allzero;
        }

        private set
        {
            allzero = value;
        }
    }
    public bool AllFull
    {
        get
        {
            return allfull;
        }

        private set
        {
            allfull = value;
        }
    }
    float Part1MaxFrame_y = 360f;

    float Part1MinFrame_y;

    float Part2MaxFrame_x = 84f;

    float Part2MinFrame_x;

    float part3maxframe_z = 0;
    public float Part3MaxFrame_z
    {
        set
        {
            if (value >= Maxlength)
                part3maxframe_z = Maxlength;
            else
                part3maxframe_z = value;
        }
        get { return part3maxframe_z; }
    }

    float Part3MinFrame_z;

    float part4maxframe_z = 0;
    public float Part4MaxFrame_z
    {
        set
        {
            if (value >= Maxlength)
                part4maxframe_z = Maxlength;
            else
                part4maxframe_z = value;
        }
        get { return part4maxframe_z; }
    }
    float Part4MinFrame_z;

    float part5maxframe_z = 0;
    public float Part5MaxFrame_z
    {
        set
        {
            if (value >= Maxlength)
                part5maxframe_z = Maxlength;
            else
                part5maxframe_z = value;
        }
        get { return part5maxframe_z; }
    }
    float Part5MinFrame_z;

    float part6maxframe_z = 0;

    public float Part6MaxFrame_z
    {
        set
        {
            if (value >= Maxlength)
                part6maxframe_z = Maxlength;
            else
                part6maxframe_z = value;
        }
        get { return part6maxframe_z; }
    }
    float Part6MinFrame_z;

    float part7maxframe_z = 0;
    public float Part7MaxFrame_z
    {
        set
        {
            if (value >= Maxlength)

                part7maxframe_z = Maxlength;
            else
                part7maxframe_z = value;
        }
        get { return part7maxframe_z; }
    }

    float Part7MinFrame_z;


    public float part7currentheight
    {
        set
        {
            fifthFull = false;
            fifthZero = false;
            if (value >= Part7MaxFrame_z)
            {
                value = Part7MaxFrame_z;
                fifthFull = true;
            }
            if (value <= Part7MinFrame_z)
            {
                value = Part7MinFrame_z;
                fifthZero = true;
            }
            part7.localPosition = new Vector3(0.01634139f, 0.1338f, value);
        }
        get { return part7.localPosition.z; }
    }
    public float part6currentheight
    {
        set
        {
            fourthFull = false;
            fourthZero = false;
            if (value >= Part6MaxFrame_z)
            {
                value = Part6MaxFrame_z;
                fourthFull = true;
            }
            if (value <= Part6MinFrame_z)
            {
                value = Part6MinFrame_z;
                fourthZero = true;
            }
            part6.localPosition = new Vector3(0.01634139f, 0.129f, value);
        }
        get { return part6.localPosition.z; }
    }
    public float part5currentheight
    {
        set
        {
            thirdZero = false;
            thirdFull = false;
            if (value >= Part5MaxFrame_z)
            {
                value = Part5MaxFrame_z;
                thirdFull = true;
            }
            if (value <= Part5MinFrame_z)
            {
                value = Part5MinFrame_z;
                thirdZero = true;
            }
            part5.localPosition = new Vector3(0.01634139f, 0.129f, value);
        }
        get { return part5.localPosition.z; }
    }
    public float part4currentheight
    {
        set
        {
            secondZero = false;
            secondFull = false;
            if (value >= Part4MaxFrame_z)
            {
                value = Part4MaxFrame_z;
                secondFull = true;
            }
            if (value <= Part4MinFrame_z)
            {
                value = Part4MinFrame_z;
                secondZero = true;
            }
            part4.localPosition = new Vector3(0.01634139f, 0.035f, value);
        }
        get { return part4.localPosition.z; }
    }
    public float part3currentheight
    {
        set
        {
            firstFull = false;
            firstZero = false;
            if (value >= Part3MaxFrame_z)
            {
                value = Part3MaxFrame_z;
                firstFull = true;
            }
            if (value <= Part3MinFrame_z)
            {
                value = Part3MinFrame_z;
                firstZero = true;
            }
            part3.localPosition = new Vector3(0.01634139f, 0.1338f, value);
        }
        get { return part3.localPosition.z; }
    }
    public void Restoration()
    {
        part3currentheight = Part3MinFrame_z;
        part4currentheight = Part4MinFrame_z;
        part5currentheight = Part5MinFrame_z;
        part6currentheight = Part6MinFrame_z;
        part7currentheight = Part7MinFrame_z;
    }
    public override void Raise(float weight)
    {
        if (AllFull)
            return;
        AllZero = false;
        if (!fifthFull)
            part7currentheight += weight;
        if (fifthFull && !fourthFull)
            part6currentheight += weight;
        if (fourthFull && !thirdFull)
            part5currentheight += weight;
        if (thirdFull && !secondFull)
            part4currentheight += weight;
        if (secondFull && !firstFull)
            part3currentheight += weight;
        if (firstFull && secondFull && thirdFull && fourthFull && fifthFull)
        {
            AllFull = true;
            //if (RecordEvent.IsRecord())
            //    AddSizeEvent(transform);
        }
        OutArmLength += weight;
        if (RecordEvent.IsRecord())
        {
            TruckArmRiceData data = new TruckArmRiceData()
            {
                GameId = gameinfo.gameObjID,
                IsRice = true,
                AllZero = AllZero,
                AllFull = AllFull,
                fifth = fifthFull,
                fourth = fourthFull,
                third = thirdFull,
                second = secondFull,
                first = firstFull,
                part7currentheight = part7currentheight,
                part6currentheight = part6currentheight,
                part5currentheight = part5currentheight,
                part4currentheight = part4currentheight,
                part3currentheight = part3currentheight,
                OutArmLength = OutArmLength,
            };
            AddTruckArmRiceEvent(data);
        }
     }
    public override void SetOutArmLength(float outarmlength)
    {
        if (AllFull)
            return;
        AllZero = false;
        OutArmLength = 0f;
        if (!fifthFull)
        {
            part7currentheight += outarmlength;
            OutArmLength += part6currentheight;
            outarmlength -= part6currentheight;
        }
        if (!fourthFull)
        {
            part6currentheight += outarmlength;
            OutArmLength += part6currentheight;
            outarmlength -= part6currentheight;
        }
        if (fourthFull && !thirdFull)
        {
            part5currentheight += outarmlength;
            OutArmLength += part5currentheight;
            outarmlength -= part5currentheight;
        }
        if (thirdFull && !secondFull)
        {
            part4currentheight += outarmlength;
            OutArmLength += part4currentheight;
            outarmlength -= part4currentheight;
        }
        if (secondFull && !firstFull)
        {
            part3currentheight += outarmlength;
            OutArmLength += part3currentheight;
            outarmlength -= part3currentheight;
        }
        if (firstFull && secondFull && thirdFull && fourthFull && fifthFull)
        {
            AllFull = true;
        }
    }
    public override void Decline(float weight)
    {
        if (AllZero)
            return;
        AllFull = false;
        if (!fifthZero)
            part7currentheight -= weight;
        if (fifthZero && !fourthZero)
            part6currentheight -= weight;
        if (fourthZero && !thirdZero)
            part5currentheight -= weight;
        if (thirdZero && !secondZero)
            part4currentheight -= weight;
        if (secondZero && !firstZero)
            part3currentheight -= weight;
        if (firstZero && secondZero && thirdZero && fourthZero)
        {
            AllZero = true;
            //if (RecordEvent.IsRecord())
            //    AddSizeEvent(transform);
        }
        OutArmLength -= weight;
        if (RecordEvent.IsRecord())
        {
            TruckArmRiceData data = new TruckArmRiceData()
            {
                GameId = gameinfo.gameObjID,
                IsRice = false,
                AllZero = AllZero,
                AllFull = AllFull,
                fifth = fifthZero,
                fourth = fourthZero,
                third = thirdZero,
                second = secondZero,
                first = firstZero,
                part7currentheight = part7currentheight,
                part6currentheight = part6currentheight,
                part5currentheight = part5currentheight,
                part4currentheight = part4currentheight,
                part3currentheight = part3currentheight,
                OutArmLength = OutArmLength,
            };
            AddTruckArmRiceEvent(data);

        }
    }
    /// <summary>
    /// 赋值车臂最大的的高度
    /// </summary>
    /// <param name="height">高度</param>
    public override void SetHeight(float height)
    {
        var Jieshu = Mathf.FloorToInt(height / Maxlength);
        float yushu = 0;
        for (int i = 1; i <= Jieshu; i++)
        {
            if (i == Jieshu)
                yushu = height % Maxlength;
            else
                yushu = Maxlength;
            if (i == 1)
                Part3MaxFrame_z = yushu;
            if (i == 2)
                Part4MaxFrame_z = yushu;
            if (i == 3)
                Part5MaxFrame_z = yushu;
            if (i == 4)
                Part6MaxFrame_z = yushu;
            if (i == 5)
                Part7MaxFrame_z = yushu;
        }
    }
    public override void FixedSupport(IMessage message)
    {
        var gameObjID = (long)message.Sender;
        if (gameObjID == GameObjID)
        {
            ControlTruckArmFlag = true;
            // AddListener();
            CarAnimationCtrl.Down();
        }
    }

    public override void PackUpTheLiftArm(IMessage message)
    {
        var gameObjID = (long)message.Sender;
        if (gameObjID == GameObjID)
        {
            ControlTruckArmFlag = false;
            CarAnimationCtrl.Up();
            OutArmLength = 0f;
            // RemoveListener();
            HorizontalBaseSupport.localEulerAngles = Vector3.zero;
            VerticalBaseSupport.localEulerAngles = Vector3.zero;//模型穿插,调整模型预设后,这里参数也跟着修改。
            part3currentheight = Part3MinFrame_z;
            part4currentheight = Part4MinFrame_z;
            part5currentheight = Part5MinFrame_z;
            part6currentheight = Part6MinFrame_z;
            part7currentheight = Part7MinFrame_z;
            allzero = true;
            allfull = false;
        }
    }

    public override void AutoLiftTruckArm(IMessage message)
    {
    }

    public override void LeftShift_J(IMessage message)
    {

    }

    public override void LeftShift_U(IMessage message)
    {
    }

    public override void D(IMessage message)
    {
        if (selfctrl)
        {
            var gameObjID = (long)message.Sender;
            if (gameObjID == GameObjID)
            {
                //if (RecordEvent.IsRecord())
                //    CreateRotationEvent(HorizontalBaseSupport);
                var speed = ((WeightCmdArgs)message.Data).Weight;
                HorizontalBaseSupport.localEulerAngles += new Vector3(0, speed, 0);
                if (RecordEvent.IsRecord())
                {
                    TruckArmRotateData data = new TruckArmRotateData()
                    {
                        GameId = gameinfo.gameObjID,
                        ArmType = RotateArmType.HorizontalBaseSupport,
                        EulerAngle = HorizontalBaseSupport.localEulerAngles,
                    };
                    AddTruckArmRotateEvent(data);
                }
            }
        }
    }

    public override void A(IMessage message)
    {
        if (selfctrl)
        {
            var gameObjID = (long)message.Sender;
            if (gameObjID == GameObjID)
            {
                var speed = ((WeightCmdArgs)message.Data).Weight;
                HorizontalBaseSupport.localEulerAngles -= new Vector3(0, speed, 0);
                if (RecordEvent.IsRecord())
                {
                    TruckArmRotateData data = new TruckArmRotateData()
                    {
                        GameId = gameinfo.gameObjID,
                        ArmType = RotateArmType.HorizontalBaseSupport,
                        EulerAngle = HorizontalBaseSupport.localEulerAngles,
                    };
                    AddTruckArmRotateEvent(data);
                }
            }
        }
    }

    public override void S(IMessage message)
    {
        if (selfctrl)
        {
            var gameObjID = (long)message.Sender;
            if (gameObjID == GameObjID)
            {
                var speed = ((WeightCmdArgs)message.Data).Weight;
                float x = VerticalBaseSupport.localEulerAngles.x;
                float y = VerticalBaseSupport.localEulerAngles.y;
                x = CheckValue(x);
                x += speed;
                x = Mathf.Clamp(x, -84, 0);//模型穿插,调整模型预设后,这里参数也跟着修改。
                VerticalBaseSupport.localEulerAngles = new Vector3(x, y, 0);
                if (RecordEvent.IsRecord())
                {
                    TruckArmRotateData data = new TruckArmRotateData()
                    {
                        GameId = gameinfo.gameObjID,
                        ArmType = RotateArmType.VerticalBaseSupport,
                        EulerAngle = VerticalBaseSupport.localEulerAngles,
                    };
                    AddTruckArmRotateEvent(data);
                }
            }
        }
    }

    public override void W(IMessage message)
    {
        if (selfctrl)
        {
            var gameObjID = (long)message.Sender;
            if (gameObjID == GameObjID)
            {
                var speed = ((WeightCmdArgs)message.Data).Weight;
                float x = VerticalBaseSupport.localEulerAngles.x;
                float y = VerticalBaseSupport.localEulerAngles.y;
                x = CheckValue(x);
                x -= speed;
                x = Mathf.Clamp(x, -84, 0);
                VerticalBaseSupport.localEulerAngles = new Vector3(x, y, 0);
                if (RecordEvent.IsRecord())
                {
                    TruckArmRotateData data = new TruckArmRotateData()
                    {
                        GameId = gameinfo.gameObjID,
                        ArmType = RotateArmType.VerticalBaseSupport,
                        EulerAngle = VerticalBaseSupport.localEulerAngles,
                    };
                    AddTruckArmRotateEvent(data);
                }
            }
        }
    }

    public override void LeftShift_S(IMessage message)
    {
        if (selfctrl)
        {
            var gameObjID = (long)message.Sender;
            if (gameObjID == GameObjID)
            {
                //if (RecordEvent.IsRecord() && !AllZero)
                //    CreateSizeEvent(this.transform);
                var weight = ((WeightCmdArgs)message.Data).Weight / 4f;
                Decline(weight);
            }
        }
    }

    public override void LeftShift_W(IMessage message)
    {
        if (selfctrl)
        {
            var gameObjID = (long)message.Sender;
            if (gameObjID == GameObjID)
            {
                //if (RecordEvent.IsRecord() && !AllFull)
                //    CreateSizeEvent(this.transform);
                var weight = ((WeightCmdArgs)message.Data).Weight / 4f;
                Raise(weight);
            }
        }
    }
    public override void ReplayArmRice(TruckArmRiceData data)
    {
        if (data.IsRice)
        {
            AllZero = data.AllZero;
            AllFull = data.AllFull;
            fifthFull = data.fifth;
            fourthFull = data.fourth;
            thirdFull = data.third;
            secondFull = data.second;
            firstFull = data.first;
            part7currentheight = data.part7currentheight;
            part6currentheight = data.part6currentheight;
            part5currentheight = data.part5currentheight;
            part4currentheight = data.part4currentheight;
            part3currentheight = data.part3currentheight;
            OutArmLength = data.OutArmLength;
        }
        else
        {
            AllZero = data.AllZero;
            AllFull = data.AllFull;
            fifthZero = data.fifth;
            fourthZero = data.fourth;
            thirdZero = data.third;
            secondZero = data.second;
            firstZero = data.first;
            part7currentheight = data.part7currentheight;
            part6currentheight = data.part6currentheight;
            part5currentheight = data.part5currentheight;
            part4currentheight = data.part4currentheight;
            part3currentheight = data.part3currentheight;
            OutArmLength = data.OutArmLength;
        }
    }
    public override void ReplayArmRotate(TruckArmRotateData data)
    {
        if (data.ArmType == RotateArmType.LastArm)
        {
            LastArm.localEulerAngles = data.EulerAngle;
        }
        else if (data.ArmType == RotateArmType.HorizontalBaseSupport)
        {
            HorizontalBaseSupport.localEulerAngles = data.EulerAngle;
        }
        else if (data.ArmType == RotateArmType.VerticalBaseSupport)
        {
            VerticalBaseSupport.localEulerAngles = data.EulerAngle;
        }
    }
}