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;

namespace DataOrganizer
{
    public partial class ProfileManager : Form
    {
        private string MachineNameLabelText { get; } = "The Machine Name property determines what\r\nprofile will be loaded automatically (Currently: '{0}').";
        private bool IsUpdating { get; set; }

        public ProfileManager()
        {
            InitializeComponent();

            ProfilesComboBox.SelectedIndexChanged += ProfilesComboBox_SelectedIndexChanged;
            ProfileNameTextBox.TextChanged += ToggleAddOrUpdateButton;
            NewContentLocationTextBox.TextChanged += ToggleAddOrUpdateButton;
            MachineNameTextBox.TextChanged += MachineNameTextBox_TextChanged;

            UpdateProfileComboBox();
        }

        private void MachineNameTextBox_TextChanged(object sender, EventArgs e)
        {
            if (MachineNameTextBox.Text.Length > 0)
                MachineNameLabel.Text = string.Format(MachineNameLabelText, MachineNameTextBox.Text);
            else
                MachineNameLabel.Text = string.Format(MachineNameLabelText, Environment.MachineName);
        }

        private void ToggleAddOrUpdateButton(object sender, EventArgs e)
        {
            if (ProfileNameTextBox.Text != string.Empty && NewContentLocationTextBox.Text != string.Empty)
                AddOrUpdateProfileButton.Enabled = true;
            else 
                AddOrUpdateProfileButton.Enabled = false;
        }

        private void UpdateProfileComboBox()
        {
            var db = new DB();

            var profiles = db.GetProfiles();
            ProfilesComboBox.Items.Clear();
            ProfilesComboBox.Tag = null;
            ProfilesComboBox.Items.Add("Add New Profile");
            ProfilesComboBox.SelectedIndex = 0;

            if (profiles.Count == 0)
            {
                ProfilesComboBox.Enabled = false;
                return;
            }

            ProfilesComboBox.Enabled = true;

            foreach (var profile in profiles)
            {
                ProfilesComboBox.Items.Add(profile.Name);
            }

            ProfilesComboBox.Tag = profiles;
        }

        private void AddOrUpdateProfileButton_Click(object sender, EventArgs e)
        {
            var machineName = Environment.MachineName;
            var db = new DB();

            if(!string.IsNullOrWhiteSpace(MachineNameTextBox.Text))
            {
                machineName = MachineNameTextBox.Text;
            }

            if (!IsUpdating)
            {
                db.InsertNewProfile(new DB.Profile { Name = ProfileNameTextBox.Text, DefaultNewContentFolder = NewContentLocationTextBox.Text, MachineName = machineName });

                UpdateProfileComboBox();
            }
            else
            {
                var profile = ((List<DB.Profile>)ProfilesComboBox.Tag)[ProfilesComboBox.SelectedIndex - 1];
                profile.Name = ProfileNameTextBox.Text;
                profile.DefaultNewContentFolder = NewContentLocationTextBox.Text;
                profile.MachineName = MachineNameTextBox.Text;
                db.UpdateProfile(profile);
            }

            UpdateProfileComboBox();
        }

        private void ProfilesComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProfilesComboBox.SelectedIndex == 0)
            {
                ProfileNameTextBox.Text = string.Empty;
                NewContentLocationTextBox.Text = string.Empty;
                MachineNameTextBox.Text = string.Empty;
                AddOrUpdateProfileButton.Text = "Add";
                AddOrUpdateProfileButton.Enabled = false;
                MachineNameLabel.Text = string.Format(MachineNameLabelText, Environment.MachineName);
                IsUpdating = false;
                return;
            }

            var profile = ((List<DB.Profile>)ProfilesComboBox.Tag)[ProfilesComboBox.SelectedIndex - 1];

            ProfileNameTextBox.Text = profile.Name;
            NewContentLocationTextBox.Text = profile.DefaultNewContentFolder;
            MachineNameTextBox.Text = profile.MachineName;

            MachineNameLabel.Text = string.Format(MachineNameLabelText, profile.MachineName);

            AddOrUpdateProfileButton.Text = "Update";
            IsUpdating = true;
        }
    }
}
