using UnityEngine;

public class MouseDrag : MonoBehaviour
{
    public Transform ForearmL;
    public Transform HandL;
    public Vector3 HandLangles;
    public int ido;
    public bool initFlg;
    public bool initFlg2;
    public bool isIdo;
    public bool isMouseDown;
    public bool isMouseDrag;
    public bool isMouseUp;
    public bool isPlay;
    public bool isSelect;
    public bool isStop;
    public Maid maid;
    public GameObject obj;
    public Vector3 off;
    public Vector3 off2;
    public bool onFlg;
    public bool onFlg2;
    public bool reset;
    public float shoki = -1000f;
    public Transform UpperArmL;
    public Vector3 UpperArmLangles;
    private readonly GameObject ForearmL2 = new GameObject();
    private readonly GameObject HandL2 = new GameObject();
    private readonly TBody.IKCMO IK = new TBody.IKCMO();
    private Vector3 mouseIti;
    private Vector3 mouseIti2;
    private Vector3 rotate;
    private Vector3 rotate2;
    private readonly GameObject UpperArmL2 = new GameObject();
    private Vector3 worldPoint;

    public void OnMouseDown()
    {
        if (!(maid != null))
            return;
        IKCtrlData ikData = maid.body0.IKCtrl.GetIKData("左手", false);
        worldPoint = Camera.main.WorldToScreenPoint(transform.position);
        off = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z));
        off2 = new Vector3(obj.transform.position.x - HandL.position.x,
                           obj.transform.position.y - HandL.position.y,
                           obj.transform.position.z - HandL.position.z);
        if (!initFlg)
        {
            IK.Init(UpperArmL, ForearmL, HandL, maid.body0);
            initFlg = true;
            if (!initFlg2)
            {
                initFlg2 = true;
                HandL2.transform.position = HandL.position;
                UpperArmL2.transform.position = UpperArmL.position;
                ForearmL2.transform.position = ForearmL.position;
                HandL2.transform.localRotation = HandL.localRotation;
                UpperArmL2.transform.localRotation = UpperArmL.localRotation;
                ForearmL2.transform.localRotation = ForearmL.localRotation;
            }
        }

        if (onFlg)
        {
            onFlg2 = true;
            IK.Init(UpperArmL, ForearmL, HandL, maid.body0);
        }

        mouseIti = Input.mousePosition;
        isPlay = maid.body0.m_Bones.GetComponent<Animation>().isPlaying;
        isSelect = true;
        rotate = HandL.localEulerAngles;
        rotate2 = UpperArmL.localEulerAngles;
        isMouseUp = false;
        isMouseDown = true;
        if (ido == 10)
        {
            Vector3 tgt = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)) + off
                          - off2;
            IK.Init(UpperArmL2.transform, ForearmL2.transform, HandL2.transform, maid.body0);
            for (int index = 0; index < 10; ++index)
            {
                IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
                IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
            }
        }

        if (shoki != -1000.0)
            return;
        shoki = UpperArmL.localEulerAngles.x;
        if (shoki <= 300.0)
            return;
        shoki -= 360f;
    }

    public void OnMouseUp()
    {
        isMouseUp = true;
        isMouseDown = false;
    }

    public void OnMouseDrag()
    {
        if (!(maid != null))
            return;
        if (isPlay && mouseIti != Input.mousePosition)
        {
            maid.body0.m_Bones.GetComponent<Animation>().Stop();
            isStop = true;
            isPlay = false;
        }

        if (reset)
        {
            reset = false;
            worldPoint = Camera.main.WorldToScreenPoint(transform.position);
            off = transform.position
                  - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z));
            off2 = new Vector3(obj.transform.position.x - HandL.position.x,
                               obj.transform.position.y - HandL.position.y,
                               obj.transform.position.z - HandL.position.z);
            rotate = HandL.localEulerAngles;
            rotate2 = UpperArmL.localEulerAngles;
            mouseIti = Input.mousePosition;
            if (onFlg)
                IK.Init(UpperArmL, ForearmL, HandL, maid.body0);
        }

        if (mouseIti != Input.mousePosition)
        {
            Vector3 tgt = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)) + off
                          - off2;
            IKCtrlData ikData = maid.body0.IKCtrl.GetIKData("左手", false);
            isMouseDrag = true;
            if (!isPlay)
                isStop = true;
            isIdo = false;
            if (ido == 0)
            {
                isIdo = true;
                IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
                if (!onFlg)
                    IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
                HandLangles = new Vector3(HandL.localEulerAngles.x, HandL.localEulerAngles.y, HandL.localEulerAngles.z);
                UpperArmLangles = new Vector3(UpperArmL.localEulerAngles.x, UpperArmL.localEulerAngles.y, UpperArmL.localEulerAngles.z);
            }
            else if (ido == 11)
            {
                IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
                if (!onFlg)
                    IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
                float x = UpperArmL.localEulerAngles.x;
                if (x > 250.0 + shoki && x < 352.0 + shoki)
                    x = 352f + shoki;
                if (x < 100.0 + shoki && x > 8.0 + shoki)
                    x = 8f + shoki;
                float z = UpperArmL.localEulerAngles.z;
                if (z > 140.0 && z < 240.0)
                    z = 240f;
                if (z <= 140.0 && z > 7.0)
                    z = 7f;
                HandL.localEulerAngles = new Vector3(rotate.x, rotate.y, HandL.localEulerAngles.z);
                UpperArmL.localEulerAngles = new Vector3(x, rotate2.y, z);
            }
            else if (ido == 12)
            {
                IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
                if (!onFlg)
                    IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
                float x = UpperArmL.localEulerAngles.x;
                if (x > 250.0 + shoki && x < 357.0 + shoki)
                    x = 357f + shoki;
                if (x < 100.0 + shoki && x > 3.0 + shoki)
                    x = 3f + shoki;
                float z = UpperArmL.localEulerAngles.z;
                if (z > 140.0 && z < 270.0)
                    z = 270f;
                if (z <= 140.0 && z > 15.0)
                    z = 15f;
                HandL.localEulerAngles = new Vector3(rotate.x, rotate.y, HandL.localEulerAngles.z);
                UpperArmL.localEulerAngles = new Vector3(x, rotate2.y, z);
            }
            else if (ido == 17)
            {
                IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
                if (!onFlg)
                    IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
                float x = UpperArmL.localEulerAngles.x;
                if (x > 250.0 + shoki && x < 357.0 + shoki)
                    x = 357f + shoki;
                if (x < 100.0 + shoki && x > 3.0 + shoki)
                    x = 3f + shoki;
                float z = UpperArmL.localEulerAngles.z;
                if (z > 220.0 && z < 345.0)
                    z = 345f;
                if (z <= 220.0 && z > 90.0)
                    z = 90f;
                HandL.localEulerAngles = new Vector3(rotate.x, rotate.y, HandL.localEulerAngles.z);
                UpperArmL.localEulerAngles = new Vector3(x, rotate2.y, z);
            }
            else if (ido == 13)
            {
                IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
                if (!onFlg)
                    IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
                float num1 = UpperArmL.localEulerAngles.x;
                if (num1 > 250.0 && num1 < 345.0)
                    num1 = 345f;
                if (num1 <= 250.0)
                    ;
                float z = UpperArmL.localEulerAngles.z;
                if (z > 160.0 && z < 275.0)
                    z = 275f;
                if (z <= 160.0 && z > 60.0)
                    z = 60f;
                float num2 = UpperArmL.localEulerAngles.y;
                if (num2 > 250.0 && num2 < 345.0)
                    num2 = 345f;
                if (num2 <= 250.0)
                    ;
                HandL.localEulerAngles = new Vector3(rotate.x, HandL.localEulerAngles.y, HandL.localEulerAngles.z);
                UpperArmL.localEulerAngles = new Vector3(UpperArmL.localEulerAngles.x, UpperArmL.localEulerAngles.y, z);
            }
            else if (ido == 14)
            {
                IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
                if (!onFlg)
                    IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
                float x = UpperArmL.localEulerAngles.x;
                if (x > 250.0 + shoki && x < 345.0 + shoki)
                    x = 345f + shoki;
                if (x < 100.0 + shoki && x > 15.0 + shoki)
                    x = 15f + shoki;
                float z = UpperArmL.localEulerAngles.z;
                if (z > 150.0 && z < 240.0)
                    z = 240f;
                if (z <= 150.0 && z > 30.0)
                    z = 30f;
                HandL.localEulerAngles = new Vector3(rotate.x, rotate.y, HandL.localEulerAngles.z);
                UpperArmL.localEulerAngles = new Vector3(x, rotate2.y, z);
            }
            else if (ido == 15)
            {
                IK.Porc(UpperArmL, ForearmL, HandL, tgt, new Vector3(), ikData);
                if (!onFlg)
                    IK.Porc(UpperArmL, ForearmL, HandL, tgt + (tgt - HandL.position), new Vector3(), ikData);
                float x = UpperArmL.localEulerAngles.x;
                if (x > 250.0 + shoki && x < 357.0 + shoki)
                    x = 357f + shoki;
                if (x < 100.0 + shoki && x > 3.0 + shoki)
                    x = 3f + shoki;
                float z = UpperArmL.localEulerAngles.z;
                if (z > 150.0 && z < 270.0)
                    z = 270f;
                if (z <= 150.0 && z > 30.0)
                    z = 30f;
                HandL.localEulerAngles = new Vector3(rotate.x, rotate.y, HandL.localEulerAngles.z);
                UpperArmL.localEulerAngles = new Vector3(x, rotate2.y, z);
            }
            else if (ido == 16)
            {
                Vector3 vector3 = Input.mousePosition - mouseIti;
                Transform transform = GameMain.Instance.MainCamera.gameObject.transform;
                transform.TransformDirection(Vector3.right);
                transform.TransformDirection(Vector3.forward);
                if (mouseIti2 != Input.mousePosition)
                {
                    UpperArmL.localEulerAngles = rotate2;
                    UpperArmL.localRotation = Quaternion.Euler(UpperArmL.localEulerAngles)
                                              * Quaternion.AngleAxis(vector3.x / 2.2f, Vector3.right);
                }
            }
            else
            {
                Vector3 vector3_1 = Input.mousePosition - mouseIti;
                Transform transform = GameMain.Instance.MainCamera.gameObject.transform;
                Vector3 vector3_2 = transform.TransformDirection(Vector3.right);
                Vector3 vector3_3 = transform.TransformDirection(Vector3.forward);
                if (mouseIti2 != Input.mousePosition)
                {
                    if (ido <= 4)
                        HandL.localEulerAngles = rotate;
                    else
                        UpperArmL.localEulerAngles = rotate2;
                    if (ido == 1)
                    {
                        HandL.localRotation = Quaternion.Euler(HandL.localEulerAngles)
                                              * Quaternion.AngleAxis(vector3_1.x / 1.5f, Vector3.up);
                        HandL.localRotation = Quaternion.Euler(HandL.localEulerAngles)
                                              * Quaternion.AngleAxis(vector3_1.y / 1.5f, Vector3.forward);
                    }

                    if (ido == 2)
                        HandL.localRotation = Quaternion.Euler(HandL.localEulerAngles)
                                              * Quaternion.AngleAxis(vector3_1.x / 1.5f, Vector3.right);
                    if (ido == 3)
                    {
                        HandL.RotateAround(HandL.position, new Vector3(vector3_2.x, 0.0f, vector3_2.z), vector3_1.y / 1f);
                        HandL.RotateAround(HandL.position, new Vector3(vector3_3.x, 0.0f, vector3_3.z), vector3_1.x / 1.5f);
                    }

                    if (ido == 4)
                        HandL.localRotation = Quaternion.Euler(HandL.localEulerAngles)
                                              * Quaternion.AngleAxis((float) (-(double) vector3_1.x / 1.5), Vector3.right);
                    if (ido == 5)
                        UpperArmL.localRotation = Quaternion.Euler(UpperArmL.localEulerAngles)
                                                  * Quaternion.AngleAxis((float) (-(double) vector3_1.x / 1.5), Vector3.right);
                }
            }
        }

        mouseIti2 = Input.mousePosition;
    }
}