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

namespace CobXmlSupport
{
    public partial class CheckWorkItems : Form
    {

        public Dictionary<string,GenSetting> FileList { get; set; }
        public CheckWorkItems()
        {
            InitializeComponent();
        }

        private void CheckWorkItems_Load(object sender, EventArgs e)
        {
            foreach (string file in FileList.Keys)
            {
                lbWorkFiles.Items.Add(file);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lbWorkFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            string fnameChosen =(string)lbWorkFiles.Items[lbWorkFiles.SelectedIndex];
            GenSetting lSetting;
            FileList.TryGetValue(fnameChosen, out lSetting);
            if (lSetting.ActualFile == S.ettingActualFile)
            {
                lSetting.GetSettings();
            }
            lvGenFiles.Items.Clear();
            if (lSetting.fileList != null)
            {
                foreach (savedFile sf in lSetting.fileList)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = sf.usageName;
                    lvi.SubItems.Add(sf.saveDate.ToLocalTime().ToString());
                    lvi.SubItems.Add(sf.fNameSaved);
                    lvi.SubItems.Add(sf.directorySaved);
                    lvGenFiles.Items.Add(lvi);
                }
            }
            else
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = "Inga filer genererade !";
                lvi.SubItems.Add("---");
                lvi.SubItems.Add("---");
                lvi.SubItems.Add("---");
                lvGenFiles.Items.Add(lvi);
            }
        }

        private void lvGenFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvGenFiles.SelectedItems.Count > 0)
            {
                ListViewItem fnameChosen;
                try
                {
                    fnameChosen = lvGenFiles.SelectedItems[0];
                    if (!fnameChosen.Text.StartsWith("Inga"))
                    {
                        showGeneratedFile(fnameChosen.Text, fnameChosen.SubItems[3].Text + "\\" + fnameChosen.SubItems[2].Text);
                    }
                    else MessageBox.Show(fnameChosen.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Problem listView :\r\n" + ex.Message);
                }
            }

        }

        private void showGeneratedFile(string usageName,string fname){
            try
            {
                ShowCode FileWin = new ShowCode();
                FileWin.FormParent = null;
                //FileWin.Labeltext = usageName;
                FileWin.Labeltext = fname;
                FileWin.CodeShower.Text = File.ReadAllText(fname);
                FileWin.ShowDialog();
 
            }
            catch (Exception ex)
            {
                MessageBox.Show("Misslyckad filläsning!\r\n"+ex.Message);
            }
        }

    }
}
