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

public class ShipDetailsUI : BaseUI
{
	[SerializeField]GameObject cargoPanel = null; // Set from editor
	[SerializeField]GameObject artifactsPanel = null; // Set from editor
	[SerializeField]GameObject equipPanel = null; // Set from editor

	[SerializeField]CargoCellController cargoCell = null; // Set from editor
	List<CargoCellController> cargoCells = new List<CargoCellController>();

	public void OnStarSystemClick()
	{
		CameraController.SetNeedDistance (30);
		BaseUI.AsyncShowWithLoaderUI<StarSystemUI, LoaderUI> (0, () => {
			Game.SetGameState (1);
		});
	}
	
	public void OnCargoClick()
	{
	}
	
	public void OnArtifactsClick() // Journal?
	{
	}
	
	public void OnEquipClick()
	{
	}

	protected override void OnShow ()
	{
		RefreshCargo ();
	}

	CargoCellController CreateCargoItem(string name, int value)
	{
		if (cargoCell == null)
		{
			return null;
		}
		cargoCell.gameObject.SetActive (false);
		CargoCellController cargo = Instantiate<CargoCellController> (cargoCell);
		cargo.GetComponent<RectTransform>().SetParent(cargoPanel.transform);
		cargo.transform.localScale = Vector3.one;
		cargo.gameObject.SetActive (true);
		cargo.SetCargoParams (name, value);
		return cargo;
	}

	void RefreshCargo()
	{
		foreach (var cargo in cargoCells)
		{
			if (cargo != null)
			{
				Destroy(cargo.gameObject);
			}
		}
		cargoCells.Clear ();
		foreach (var resource in Player.info.ResourceList)
		{
			cargoCells.Add(CreateCargoItem(resource.name, resource.value));
		}
	}

}
