using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace friction
{
	public partial class PanelMaterial : DockContent
	{
		MainForm m_Owner = null;

		ListViewItem m_SelectedSpring = null;
		ListViewItem m_SelectedTable = null;

		public PanelMaterial(MainForm owner)
		{
			InitializeComponent();

			m_Owner = owner;
			this.ApplyTheme();

			Theme.Apply(this);
			Theme.Apply(lvSpring);
			Theme.Apply(lvTable);
		}

		public void UpdateData(DataHandler data)
		{
			m_SelectedSpring = null;
			m_SelectedTable = null;

			var SpringList = data.GetSpringList();
			var TableList = data.GetTableList();

			lvSpring.Items.Clear();
			foreach (var x in SpringList)
				lvSpring.Items.Add(x);
				
			lvTable.Items.Clear();
			lvTable.Items.Add("All");
			foreach (var x in TableList)
				lvTable.Items.Add(x);

			lvSpring.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
			lvTable.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);

			lbFileName.Text = data.GetFileName();

			SelectSpring(0);
			SelectTable(0);
		}

		private void lvSpring_SizeChanged(object sender, EventArgs e)
		{
			Theme.ResizeFullColumn(lvSpring);
		}

		private void lvTable_SizeChanged(object sender, EventArgs e)
		{
			Theme.ResizeFullColumn(lvTable);
		}

		private void SelectSpring(int iIdx)
		{
			if (m_SelectedSpring != null)
			{
				m_SelectedSpring.Font = lvSpring.Font;
				m_SelectedSpring.ForeColor = Theme.Forecolor;
			}

			m_SelectedSpring = lvSpring.Items[iIdx];
			m_SelectedSpring.Font = new Font(lvSpring.Font, FontStyle.Bold);
			m_SelectedSpring.ForeColor = Color.Orange;

			if(m_SelectedSpring != null && m_SelectedTable != null)
				m_Owner.OnApplyData(m_SelectedSpring.Text, m_SelectedTable.Text);
		}

		private void SelectTable(int iIdx)
		{
			if (m_SelectedTable != null)
			{
				m_SelectedTable.Font = lvTable.Font;
				m_SelectedTable.ForeColor = Theme.Forecolor;
			}

			m_SelectedTable = lvTable.Items[iIdx];
			m_SelectedTable.Font = new Font(lvTable.Font, FontStyle.Bold);
			m_SelectedTable.ForeColor = Color.Orange;

			if (m_SelectedSpring != null && m_SelectedTable != null)
				m_Owner.OnApplyData(m_SelectedSpring.Text, m_SelectedTable.Text);
		}

		private void lvSpring_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if(lvSpring.SelectedIndices.Count == 1)
				SelectSpring(lvSpring.SelectedIndices[0]);
		}

		private void lvTable_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (lvTable.SelectedIndices.Count == 1)
				SelectTable(lvTable.SelectedIndices[0]);
		}
	}
}
