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 System.Text.RegularExpressions;

using LabelerCore.Loop;
using LabelerCore.Data;


namespace Labeler
{
    public partial class ClassifierForm : Form {

        private CppLoopClassifier _cppLoopClf;
        public bool IsFitted { get { return _cppLoopClf != null; } }


        public ClassifierForm() {
            InitializeComponent();
        }

        public int[] Predict(double[][] X){
            if (IsFitted)
                return _cppLoopClf.Predict(X);
            else
                return null;
        }

        public CppNeighbor[] GetNeighbors(double[] x, uint numNeighbors=10) {
            return _cppLoopClf.GetNeighbors(x, numNeighbors);
        }

        public string ClassificationInfo(double[] x) {
            return _cppLoopClf.ClassificationInfo(x);
        }

        private void loadDataToolStripMenuItem_Click(object sender, EventArgs e) {
            var fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Classifier|*.xml";
            var res = fileDialog.ShowDialog();
            if (res == System.Windows.Forms.DialogResult.OK) {
                LoadClassifier(fileDialog.FileName);
            }
        }

        public void InitDGV() {
            //http://stackoverflow.com/questions/4646920/populating-a-datagridview-with-text-and-progressbars

            DataGridViewProgressColumn column = new DataGridViewProgressColumn();

            DGVClfRes.ColumnCount = 2;
            DGVClfRes.Columns[0].Name = "Class Code";
            DGVClfRes.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            DGVClfRes.Columns[1].Name = "Classes";
            DGVClfRes.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            DGVClfRes.Columns.Add(column);
            DGVClfRes.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            column.HeaderText = "Probability";

            //object[] row1 = new object[] { "test1", "test2", 50 };
            //object[] row2 = new object[] { "test1", "test2", 55 };
            //object[] row3 = new object[] { "test1", "test2", 22 };
            //object[] rows = new object[] { row1, row2, row3 };

            //foreach (object[] row in rows) {
            //    DGVClfRes.Rows.Add(row);
            //}
        }

        public void DispProbs(double[] x) {
            DGVClfRes.Rows.Clear();
            Tuple<int, string>[] classes = _cppLoopClf.GetClasses();
            Tuple<int, double>[] probs = _cppLoopClf.GetProbs(x);
            for (int i = 0; i < classes.Length; i++) {
                double[] t = probs.Where(p => p.Item1 == classes[i].Item1)
                                   .Select(p => p.Item2)
                                   .ToArray();
                double prob = 0.0;
                if (t.Length > 0)
                    prob = t[0];
                DGVClfRes.Rows.Add(new object[] { classes[i].Item1, //classes[i].Item1,
                                                  classes[i].Item2, //classes[i].Item2, 
                                                 (int)(Math.Round(prob * 100.0)) });
            }


        }

        public void LoadClassifier(string FileName) {
            try {
                _cppLoopClf = new CppLoopClassifier(FileName);
                this.Text = System.IO.Path.GetFileName(FileName);
                InitDGV();
            }
            catch {
                MessageBox.Show("Can't load " + FileName);
                _cppLoopClf = null;
            }

        }

    }
}
