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 LabelerCore.Data;
using LabelerCore.Loop;

namespace Labeler {
    public partial class DatasetConverterForm : Form {
        public DatasetConverterForm() {
            InitializeComponent();
        }

        private void textBox1_Click(object sender, EventArgs e) {
            var fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Dataset|*.csv";
            var res = fileDialog.ShowDialog();
            if (res == System.Windows.Forms.DialogResult.OK) {
                csvTextBox.Text = fileDialog.FileName;
                csvTextBox.ForeColor = Color.Black;
            }  
        }

        private void xmlTextBox_MouseClick(object sender, MouseEventArgs e) {
            var fileDialog = new SaveFileDialog();
            fileDialog.Filter = "Dataset|*.xml";
            var res = fileDialog.ShowDialog();
            if (res == System.Windows.Forms.DialogResult.OK) {
                xmlTextBox.Text = fileDialog.FileName;
                xmlTextBox.ForeColor = Color.Black;
            }
        }

        private void convertToButton_Click(object sender, EventArgs e) {
            if (xmlTextBox.Text == "") {
                MessageBox.Show("Select path to save");
            }
            try {
                TrainingData tD = new TrainingData(csvTextBox.Text,
                                        @"",
                                        deepLoad: false);

                CppDataset cppDataset = new CppDataset();
                foreach (Sample s in tD) {
                    cppDataset.AddSample(s.vehicleClass, s.path, s.FeatureArray);
                }
                cppDataset.SaveTo(xmlTextBox.Text);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
