using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class MoveAction : IAction {
    Vector3 startPosition;
    Vector3 targetPosition;
    private Creature toMove;
    private Creature targetCreature;
    private bool shouldMove = false;
    private bool moveToPosition = false;

    private int movedSquares = 0;

    public void SetStartPosition(Vector3 startPos) {
        startPosition = startPos;
    }

    public void SetTargetPosition(Vector3 targetPos) {
        targetPosition = targetPos;
        moveToPosition = true;
    }

    public void SetTargetCreature(Creature target) {
        targetCreature = target;
        moveToPosition = false;
    }

    public void SetCreatureToMove(Creature toMove) {
        this.toMove = toMove;
    }

    /*
        * If someone is within 5 feet, should you stop and attack?
            If not, attack of opportunity?
        * Try not to move within 5 feet of non target, and still closing in to target (prio)
        * När toMove är innom 5 foot av target, avbryt move och attakera direkt?
            eller flytta efter till end of movement, och då attackera, även om target fortsätter att flytta sig.
    */
    public IEnumerator PerformAction() {
        Tilemap tileMap = GameManagerScript.GetInstance().TileMap;
        if (moveToPosition) {
            Vector3 distance = tileMap.WorldToCell(targetPosition - startPosition);
            int safety = 0;
            while (!distance.Equals(Vector3.zero)) {
                safety++;
                if (distance.x != 0 || distance.y != 0) {
                    Vector3 movement = toMove.transform.position;
                    if (distance.x > 0) {
                        movement.x += 1;
                        distance.x -= 1;
                    } else if (distance.x < 0) {
                        movement.x -= 1;
                        distance.x += 1;
                    }

                    if (distance.y > 0) {
                        movement.y += 1;
                        distance.y -= 1;
                    } else if (distance.y < 0) {
                        movement.y -= 1;
                        distance.y += 1;
                    }

                    checkIfOccupoied(movement);

                    toMove.transform.position = movement;
                    movedSquares++;
                    if (toMove.movementRate <= movedSquares * 5) { // this one have moved all that they can for one action
                        Debug.Log(toMove.name + " moved there entire " + toMove.movementRate + " distance, stopping");
                        break;
                    }
                    yield return new WaitForSeconds(1);
                }
                if (safety > 120) {
                    throw new System.Exception("LOOPED 120 times");
                }
            }
        } else { // move to creature
            Vector3 distance = tileMap.WorldToCell(targetCreature.transform.position - startPosition);
            int safety = 0;
            while (!distance.Equals(Vector3.zero)) {
                safety++;
                if (distance.x != 0 || distance.y != 0) { // Move diagonally
                    Vector3 movement = toMove.transform.position;
                    if (distance.x > 0) {
                        movement.x += 1;
                        distance.x -= 1;
                    } else if (distance.x < 0) {
                        movement.x -= 1;
                        distance.x += 1;
                    }

                    if (distance.y > 0) {
                        movement.y += 1;
                        distance.y -= 1;
                    } else if (distance.y < 0) {
                        movement.y -= 1;
                        distance.y += 1;
                    }
                    toMove.transform.position = movement;
                    movedSquares++;
                    if (toMove.movementRate <= movedSquares * 5) { // this one have moved all that they can for one action
                        Debug.Log(toMove.name + " moved there entire " + toMove.movementRate + " distance, stopping");
                        break;
                    }
                    yield return new WaitForSeconds(1);

                    distance = tileMap.WorldToCell(targetCreature.transform.position - toMove.transform.position);
                    Debug.Log(toMove.name + " move to " + targetPosition + "(" + tileMap.WorldToCell(targetPosition) + ") from " + startPosition + "(" + tileMap.WorldToCell(startPosition) + " by moving " + distance);
                }
                if (safety > 1000) {
                    throw new System.Exception("LOOPED 1000 times");
                }
            }
        }

        yield return null;
    }

    private void checkIfOccupoied(Vector3 movement) {

    }
}
