﻿using UnityEngine;
using System.Collections;
using ProD;

public class DungeonGen : MonoBehaviour {

	public GameObject player2DPrefab;
	public GameObject cameraGO;
	
	#region WorldMap
	private WorldMap worldMap;
	public int worldWidth = 1;
	public int worldHeight = 1;
	#endregion
	
	#region Map
	public string theme = "Stone Theme";
	public int mapWidth = 45;
	public int mapHeight = 45;
	#endregion
	
	#region Dungeon
	public int dungeon_room_Min_X = 3;
	public int dungeon_room_Max_X = 11;
	public int dungeon_room_Min_Y = 3;
	public int dungeon_room_Max_Y = 11;
	public int dungeon_room_Freq = 10;
	public int dungeon_room_Retry = 6;
	public int dungeon_doorsPerRoom = 1;
	public int dungeon_repeat = 12;
	public bool dungeon_frameMap = false;
	#endregion
	
	#region DwarfTown
	public int dwarftown_room1_Min_X = 3;
	public int dwarftown_room1_Max_X = 11;
	public int dwarftown_room1_Min_Y = 3;
	public int dwarftown_room1_Max_Y = 11;
	public int dwarftown_room1_Freq = 10;
	public int dwarftown_room1_Retry = 6;
	public int dwarftown_room2_Min_X = 3;
	public int dwarftown_room2_Max_X = 11;
	public int dwarftown_room2_Min_Y = 3;
	public int dwarftown_room2_Max_Y = 11;
	public int dwarftown_room2_Freq = 10;
	public int dwarftown_room2_Retry = 6;
	#endregion
	
	void Start()
	{
		GenerateAndMaterialize();

		setCamera ();

		setVis ();

		darkHouse ();
	}
	
	//A button in the inspector to call Generate()
	public bool RE_GenerateAndMaterialize = false;
	void Update()
	{
		if(RE_GenerateAndMaterialize)
		{
			RE_GenerateAndMaterialize = false;
			GenerateAndMaterialize();
		}
	}
	
	//Set all necessary properties, generate the world and instantiate it.
	private void GenerateAndMaterialize()
	{
		//If there was an existing map, remove the map first.
		Materializer.Instance.UnmaterializeWorldMap();
		//Set the generic  properties of the dungeon map.
		Generator_DwarfTown.SetGenericProperties(mapWidth, mapHeight, theme);
		
		
		
		Generator_DwarfTown.SetSpecificProperties (
			"Abyss", "Path", "Wall",
			dwarftown_room1_Min_X,
			dwarftown_room1_Max_X,
			dwarftown_room1_Min_Y,
			dwarftown_room1_Max_Y,
			dwarftown_room1_Freq,
			dwarftown_room1_Retry,
			dwarftown_room2_Min_X,
			dwarftown_room2_Max_X,
			dwarftown_room2_Min_Y,
			dwarftown_room2_Max_Y,
			dwarftown_room2_Freq,
			dwarftown_room2_Retry);

        //Set the specific properties of the dungeon map.
        Generator_Dungeon.SetSpecificProperties(
            "Abyss", "Path", "Wall", 
            dungeon_room_Min_X, 
            dungeon_room_Max_X, 
            dungeon_room_Min_Y, 
            dungeon_room_Max_Y, 
            dungeon_room_Freq, 
            dungeon_room_Retry, 
            dungeon_doorsPerRoom, 
            dungeon_repeat, 
            dungeon_frameMap);

			
		//Set the asset theme the world will use.
		Generator_Generic_World.theme = theme;
		//Generate the world using the dungeon generator.
		worldMap = Generator_Generic_World.Generate("DwarfTown", worldWidth, worldHeight, mapWidth, mapHeight);
		//Materialize the world via instantiating its cells.
		Materializer.Instance.MaterializeWorldMap(worldMap);
	}

	private void setCamera()
	{
		cameraGO.GetComponent<Camera>().enabled = true;
		cameraGO.GetComponent<CameraObjectTracker>().enabled = true;
	}
	
	private void setVis()
	{
		ProDManager.Instance.getFogOfWar().visibilityRange = 3;
		ProDManager.Instance.getFogOfWar ().type = FogOfWar.ShadowType.RayRoundFlood;
		ProDManager.Instance.getFogOfWar().filterMode = FilterMode.Bilinear;
		ProDManager.Instance.getFogOfWar ().InitFoW (worldMap.maps [0, 0]);
		
		ProDManager.Instance.getPathfinding().limitedTo = PathFinding.LimitedTo.VisibleOnly;
		ProDManager.Instance.getPathfinding().walkSpeed = 100;
		InputManager.Instance.allowDragClicks = true;
	}
	
	
	private void darkHouse(){
		
		ProDManager.Instance.getFogOfWar ().InitFoW (worldMap.maps [0, 0]);
		ProDManager.Instance.getPathfinding ().InitPathfinding (worldMap.maps [0, 0]);
		
		ProDManager.Instance.ApplySeed();
		ProDManager.Instance.SpawnPlayer (player2DPrefab ,worldMap);
		
	}

}
