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.IO;

namespace KonvertorSorCsv
{
    public partial class Form1 : Form
    {
        string inputfilename;
        string outputfilename;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // vstupny subor
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Sor (*.sor)|*sor|All (*.*)|*.*";
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                textBox1.Text = openFileDialog1.FileName;
                inputfilename = openFileDialog1.FileName;
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //vystupny subor
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "csv file|*.csv|text file|*.txt";
            DialogResult result = saveFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                textBox2.Text = saveFileDialog1.FileName;
                outputfilename = saveFileDialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //konvert
            if(inputfilename!=null&&outputfilename!=null)
            {
                SorParser sp = new SorParser(inputfilename);
                sp.parse();
                string outstring = "";
                List < DataPoint> points = sp.getDataPoints();
                for (int i = 0; i < points.Count; ++i)
                {
                    outstring += (points[i].x.ToString() + '\t' + points[i].y.ToString() + '\n');
                }
                FileStream fs2 = new FileStream(outputfilename, FileMode.OpenOrCreate);
                StreamWriter sw3 = new StreamWriter(fs2);
                sw3.Write(outstring);
                sw3.Dispose();
            }
        }
    }
}
