using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;

using LabelerCore.Data;
using UserControls;
using LabelerCore.Loop;
 

namespace Labeler
{
    public partial class NeighborsForm : Form
    {
        private List<Neighbor> _neighbors;
        public UserControls.RecordTable RecordTable {get { return recordTable; }}

        public TrainingData TrainData
        {
            get{ return recordTable.TrainData;  }
            set{ recordTable.TrainData = value; }
        }

        public NeighborsForm(List<Neighbor> neighbors)
        {
            InitializeComponent();
            recordTable.ItemEnter += recordTable_ItemEnter;
            _neighbors = neighbors;

        }

        private void recordTable_ItemEnter(VehicleInfo info,
                                           double[] x,
                                           int toolTipXPos,
                                           int toolTipYPos)
        {
            try {
                recordView.updateRecordView(info, false);
            }
            catch (Exception e) {
                MessageBox.Show("There was something unexpected: " + e.Message);
            }
        }

        new public void Show()
        {
            recordTable.ShowItems(_neighbors.Cast<Sample>().ToArray());
            
            //add distance column
            DataGridViewColumn col = new DataGridViewColumn();
            col.ValueType = typeof(double);
            col.Name = "distance" + "Col";
            col.HeaderText = "Distance";
            col.DataPropertyName = "Distance";
            col.ReadOnly = true;
            col.CellTemplate = new DataGridViewTextBoxCell();
            col.SortMode = DataGridViewColumnSortMode.Automatic;
            col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            recordTable.DGV.Columns.Add(col);

            int idx = 0;
            foreach (Neighbor s in _neighbors)
                recordTable.DGV[recordTable.DGV.Columns["distanceCol"].Index, idx++].Value = s.distance; 

            for(int i = 0; i < recordTable.DGV.Columns.Count; i++)
                recordTable.DGV.Columns[i].ReadOnly = true;

            base.Show();
        }

    }
}
