using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class Mouse : MonoBehaviour
{
    //Windows接口
    [DllImport("user32.dll")]
    public static extern short GetAsyncKeyState(int vKey);
    private const int VK_LBUTTON = 0x01; //鼠标左键
    private const int VK_RBUTTON = 0x02; //鼠标右键


    private Vector3 lastMousePosition = Vector3.zero;
    private bool isLMouseDown = false;
    private bool isRMouseDown = false;
    private bool removeTransYet = false;
    private bool notLClickOnObj = false;
    private bool notRClickOnObj = false;
    private static float previousLClickTime;

    void Update()
    {
        if (isMouseOnObj())
        {
            if (!removeTransYet)
            {
                removeTransYet = true;
                Window.removeMouseTransparent();
            }
        }
        else
        {
            if (removeTransYet)
            {
                removeTransYet = false;
                Window.setMouseTransparent();
            }
        }

        if (GetAsyncKeyState(VK_LBUTTON) != 0)
        {
            if (!isMouseOnObj())
            {
                notLClickOnObj = true;
            }
            else if (!notLClickOnObj && !isLMouseDown)
            {
                isLMouseDown = true;
                if (Time.time - previousLClickTime < 0.5 && Move.hugeAmount > 1)
                {
                    GameObject thisHuge = GameObject.Find(this.name);
                    Destroy(thisHuge);
                    Move.hugeAmount--;
                    Window.setMouseTransparent();
                    return;
                }
                this.GetComponent<Move>().changeState(Move.State.STOP);
                this.GetComponent<Move>().changeSide(Move.Side.CENTER);
                previousLClickTime = Time.time;
            }
        }
        else
        {
            notLClickOnObj = false;
            if (isLMouseDown)
            {
                isLMouseDown = false;
                lastMousePosition = Vector3.zero;
                if (!this.GetComponent<Move>().checkOnWall())
                {
                    this.GetComponent<Move>().changeState(Move.State.STOP);
                    this.GetComponent<Move>().changeSide(Move.Side.BOTTOM);
                }
            }
        }

        if (isLMouseDown)
        {
            if (lastMousePosition != Vector3.zero)
            {
                Vector3 offset = Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePosition;
                this.transform.position += offset;
            }
            lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }


        if (GetAsyncKeyState(VK_RBUTTON) != 0)
        {
            if (!isMouseOnObj())
            {
                notRClickOnObj = true;
            }
            else if (!notRClickOnObj && !isRMouseDown)
            {
                isRMouseDown = true;
                if (Move.previousCloneTime + 1 < Time.time)
                {
                    this.GetComponent<Move>().spawnHuge();
                }

            }
        }
        else
        {
            notRClickOnObj = false;
            if (isRMouseDown)
            {
                isRMouseDown = false;
            }
        }
    }


    public bool isMouseOnObj()
    {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
        if (hit.collider != null && hit.collider.name == this.name)
        {
            return true;
        }
        return false;
    }
}