using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using LabelerCore.Data;

using UserControls;

namespace Labeler
{
    public partial class OptionsForm : Form
    {
        private RecordTableColumns _recordTableColumns;
        public bool clfInfoDetailsToolTipEnabled;
        public bool deepLoad;

        public FeatureHeader[] Features
        {
            get
            {
                FeatureHeader[] ret = new FeatureHeader[featuresDGV.Rows.Count];
                for(int i = 0; i < featuresDGV.Rows.Count; i++)
                {
                    string fName = (string)featuresDGV[featuresDGV.Columns["FeatureNameCol"].Index, i].Value;
                    double fWeight = double.Parse((string)featuresDGV[featuresDGV.Columns["FeatureWeightCol"].Index, i].Value);
                    ret[i] = new FeatureHeader((uint)i, fName, fWeight);
                }
                return ret;
            }
        }

        public OptionsForm(RecordTableColumns recordTableColumns, 
                           bool clfInfoDetailsToolTipEnabled = false)
        {
            InitializeComponent();
            this.DialogResult = DialogResult.Cancel;
            _recordTableColumns = recordTableColumns;
            this.clfInfoDetailsToolTipEnabled = clfInfoDetailsToolTipEnabled;

            //init default cols
            attributesCheckedListBox.Items.AddRange(_recordTableColumns.columns.Select(x => x.name).ToArray());
            foreach (var col in _recordTableColumns.columns)
                attributesCheckedListBox.SetItemChecked(attributesCheckedListBox.Items.IndexOf(col.name),
                                                        col.enabled);

            clfInfoDetailsCheckBox.Checked = clfInfoDetailsToolTipEnabled;
            _updateFeaturesDGV();
        }

        private void _updateFeaturesDGV()
        {
            featuresDGV.Rows.Clear();
            featuresDGV.Rows.Add(Sample.Features.Length);
            for (int i = 0; i < Sample.Features.Length; i++)
            {
                featuresDGV[featuresDGV.Columns["FeatureNameCol"].Index, i].Value = Sample.Features[i].Name;
                featuresDGV[featuresDGV.Columns["FeatureWeightCol"].Index, i].Value = Sample.Features[i].Weight.ToString();
            }
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            for (int i = 0; i < attributesCheckedListBox.Items.Count; i++)
                _recordTableColumns.columns[i].enabled = attributesCheckedListBox.GetItemChecked(i);


            clfInfoDetailsToolTipEnabled = clfInfoDetailsCheckBox.Checked;
            deepLoad = deepLoadCheckBox.Checked;
            Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void featuresDGV_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if(e.ColumnIndex == featuresDGV.Columns["FeatureNameCol"].Index)
            {
                string fName = (string)e.FormattedValue;
                if (!attributesCheckedListBox.Items.Contains(fName))
                {
                    MessageBox.Show("Invalid features name");
                    e.Cancel = true;
                }
            }
            else if (e.ColumnIndex == featuresDGV.Columns["FeatureWeightCol"].Index)
            {

                string fWeight = (string)e.FormattedValue;
                double dummy;
                if (!double.TryParse(fWeight, out dummy))
                {
                    MessageBox.Show("Invalid features weight, should be positive float");
                    e.Cancel = true;
                }
                else if (dummy < 0.0)
                {
                    MessageBox.Show("Invalid features weight, should be positive float");
                    e.Cancel = true;
                }
            }

        }

        private void addButton_Click(object sender, EventArgs e)
        {
            featuresDGV.Rows.Add();
        }
    }
}
