﻿using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// role
/// </summary>
public class Role
{
	private int searchValue = 6;

	public int SearchValue {
		get {
			return searchValue;
		}
	}

	private int actionFrame = 5;

	public int ActionFrame {
		get {
			return actionFrame;
		}
	}

	private HeroMap heroMap;

	public HeroMap HeroMap {
		set {
			this.heroMap = value;
		}
	}

	private JobManager jobManager;

	public JobManager JobManager {
		set {
			this.jobManager = value;
		}
	}

	private RoleAction nowAction;

	private Job job;

	private MapCell enterCell;

	public int X {
		get {
			return enterCell.X;
		}
	}

	public int Y {
		get {
			return enterCell.Y;
		}
	}

	private string name = "村";

	public string Name {
		get {
			return name;
		}
	}

	private Goods carryGoods = null;

	public void CarryUpGoods (Goods goods)
	{
		this.carryGoods = goods;
	}

	public void CarryUpGoods (int count)
	{
		CarryUpGoods (enterCell.RemoveGoods (count));
	}

	public void CarryDownGoods (int count)
	{
		Goods goods;
		if (this.carryGoods.Count > count) {
			goods = carryGoods.Split (count);
		} else {
			goods = CarryDownAllGoods ();
		}
		heroMap.GetMapCell (X, Y).AddGoods (goods);
	}

	public Goods CarryDownAllGoods ()
	{
		Goods returnGoods = this.carryGoods;
		this.carryGoods = null;
		return returnGoods;
	}

	public Goods CarryGoods {
		get {
			return carryGoods;
		}
	}

	public void EnterCell (MapCell mapCell)
	{
		this.enterCell = mapCell;
	}

	public void MoveToCell (int x, int y)
	{
		heroMap.GetMapCell (X, Y).RoleLeave (this);
		heroMap.GetMapCell (x, y).RoleEnter (this);
	}

	public void Update (long startTimes)
	{
		if (nowAction != null) {
			if (nowAction.DoAction (this) == true) {
				//action finish
				nowAction = null;
			}
		} else {
			if (job != null) {
				RoleAction newAction = job.GetAction (this);
				if (newAction != null) {
					nowAction = newAction;
				} else {
					// job finish
					job = null;
				}
			} else {
				job = jobManager.GetJob ();
				if (job == null) {
					//random move
					Vector2Int pos = CrossFinder.GetPos (Random.Range (1, 5));
					pos.AddX (X);
					pos.AddY (Y);
					if (heroMap.CheckPos (pos.X, pos.Y) == true) {
						if (heroMap.MapCellCanMove (pos.X, pos.Y) == true) {
							MoveAction moveAction = new MoveAction ();
							moveAction.Init (pos, this);
							nowAction = moveAction;
						}
					}
				}
			}
		}
	}

	public Goods SearchGoods (int x, int y)
	{
		return heroMap.GetGoods (x, y);
	}

	public void FreeGoods ()
	{
		heroMap.FreeGoods (this.X, this.Y);
	}

	public void LockGoods ()
	{
		heroMap.LockGoods (this.X, this.Y);
	}

	public List<Vector2Int> GetPath (int x, int y)
	{
		return heroMap.GetPath (new Vector2Int (X, Y), new Vector2Int (x, y), this);
	}

	public int GetMoveValue (int x, int y)
	{
		return heroMap.GetMapCell (x, y).GetMoveValue (this) + actionFrame;
	}

	public bool CheckGoods (GoodsType goodsType, int number)
	{
		Goods goods = heroMap.GetMapCell (X, Y).GetGoods ();
		if (goods == null) {
			return false;
		}

		if (goods.GoodsType == goodsType && goods.Count == number) {
			return true;
		} else {
			return false;
		}
	}

	public void RemoveGoods (int number)
	{
		heroMap.GetMapCell (X, Y).RemoveGoods (number);
	}

	public void BuildGround (GroundType groundType)
	{
		heroMap.GetMapCell (X, Y).BuildGround (groundType);
	}

	public void BuildUpGround (UpGroundType upGroundType)
	{
		heroMap.GetMapCell (X, Y).BuildUpGround (upGroundType);
	}
}
