using ImGuiNET;
using skyscraper5.Skyscraper.Scraper.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using skyscraper5.Skyscraper.Equipment;
using skyscraper8.Skyscraper.Scraper.Storage;

namespace SDL2Demo.Forms
{
	internal class ConfigureDishTypes : IRenderable
	{
		private readonly DataStorage _scraperStroage;
		private List<DishEntity> dishTypes;
		private string tableUuid;

		private string manufacturer;
		private string name;
		private int diameter;
		private DishShape shape;
		public ConfigureDishTypes(DataStorage scraperStroage, List<DishEntity> dishTypes)
		{
			_scraperStroage = scraperStroage;
			this.dishTypes = dishTypes;
			this.tableUuid = Guid.NewGuid().ToString();
			this.name = "";
		}


		public void Render()
		{
			bool pOpen = true;
			ImGuiWindowFlags windowFlags = ImGuiWindowFlags.AlwaysAutoResize;
			ImGui.Begin("Configure Dish Types", ref pOpen, windowFlags);
			if (justSaved)
			{
				name = "";
				manufacturer = "";
				diameter = 0;
				shape = DishShape.Offset;
				justSaved = false;
			}
			if (ImGui.TreeNode("Add Dish Type"))
			{
				ImGui.InputText("Manufacturer", ref manufacturer, 255);
				ImGui.InputText("Name", ref name, 255);
				ImGui.InputInt("Diameter", ref diameter, 5, 10);

				if (ImGui.BeginCombo("Shape", shape.ToString()))
				{
					DishShape[] dishShapes = Enum.GetValues<DishShape>();
					foreach (DishShape dishShape in dishShapes)
					{
						bool isSelected = dishShape == shape;
						if (ImGui.Selectable(dishShape.ToString(), isSelected))
						{
							shape = dishShape;
						}
					}
					ImGui.EndCombo();
				}

				ImGui.BeginDisabled(!SaveButtonEnabled());
				if (ImGui.Button("Add"))
				{
					DishEntity newDish = new DishEntity(manufacturer, name, diameter, shape);
					dishTypes.Add(newDish);
					_scraperStroage.UiDishTypesAdd(newDish);
					justSaved = true;
				}
				ImGui.EndDisabled();
			}

			ImGui.Separator();

			ImGui.BeginTable(tableUuid, 3);
			ImGui.TableNextRow();
			ImGui.TableSetColumnIndex(0);
			ImGui.Text("Name");

			ImGui.TableSetColumnIndex(1);
			ImGui.Text("Size");

			ImGui.TableSetColumnIndex(2);
			ImGui.Text("Type");

			foreach (DishEntity dishType in dishTypes)
			{
				ImGui.TableNextRow();
				ImGui.TableSetColumnIndex(0);
				ImGui.Text(dishType.Name);

				ImGui.TableSetColumnIndex(1);
				ImGui.Text(String.Format("{0} cm", dishType.Diameter));

				ImGui.TableSetColumnIndex(2);
				ImGui.Text(dishType.Shape.ToString());
			}
			ImGui.EndTable();
			ImGui.End();

			if (!pOpen)
				Closed = true;
		}

		private bool SaveButtonEnabled()
		{
			if (string.IsNullOrEmpty(name))
				return false;

			if (string.IsNullOrWhiteSpace(name))
				return false;

			if (diameter == 0)
				return false;

			if (diameter < 0)
			{
				diameter = 0;
				return false;
			}

			foreach (DishEntity dishType in dishTypes)
			{
				if (dishType.Name.Equals(name))
					return false;
			}

			return true;
		}

		public bool Closed { get; private set; }

		private bool justSaved;
	}
}
