using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using KlassenBIB;
using System.Diagnostics;
using System.IO;
using SanShared;
using FluentFTP;
using System.Collections;

namespace SanSystem
{
    
    public partial class UCInliner : UserControl
    {
        enum TabPages
        {
            VORRAUSSETZUNGEN = 0,
            LINERGRUNDLAGEN
        }
        InlinerSanierung inliner = null;
        string destinationPath = string.Empty;
        List<string> filenames = new List<string>();
        public UCInliner(InlinerSanierung san)
        {
            InitializeComponent();
            inliner = san;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (inliner == null) throw new Exception("Inliner is null");
            TabControl tb = (TabControl)sender;
            if (tb == null) return;
            switch (tb.SelectedIndex)
            {
                
                case (int)TabPages.LINERGRUNDLAGEN:
                    txt_walze_distance.DataBindings.Clear();
                    txt_vakuum.DataBindings.Clear();
                    txt_temp_harz.DataBindings.Clear();
                    txt_charge_harz.DataBindings.Clear();
                    txt_charge_liner.DataBindings.Clear();
                    dt_kalibrierdatum.DataBindings.Clear();
                    txt_liner.DataBindings.Clear();
                    txt_harz.DataBindings.Clear();

                    txt_harz.DataBindings.Add(new Binding("Text", inliner, "HarzTyp"));
                    txt_liner.DataBindings.Add(new Binding("Text", inliner, "LinerTyp"));
                    txt_walze_distance.DataBindings.Add(new Binding("Text", inliner, "KalibierWalzenAbstand"));
                    txt_vakuum.DataBindings.Add(new Binding("Text", inliner, "KalibrierUnterdruck"));
                    txt_temp_harz.DataBindings.Add(new Binding("Text", inliner, "HarzKalibrierTemperatur"));
                    txt_charge_harz.DataBindings.Add(new Binding("Text", inliner, "HarzChargenummer"));
                    txt_charge_liner.DataBindings.Add(new Binding("Text", inliner, "LinerChargenummer"));
                    dt_kalibrierdatum.DataBindings.Add(new Binding("Value", inliner, "DatumKalibrierung"));
                    break;
            }
        }

        private void UCInliner_Load(object sender, EventArgs e)
        {
            CheckDirectories();
            cb_genehmigung.DataBindings.Clear();
            cb_stvo.DataBindings.Clear();
            cb_wasserhaltung.DataBindings.Clear();
            txt_temp_aussen.DataBindings.Clear();
            txt_temp_kanal.DataBindings.Clear();
            cbb_wetter.DataBindings.Clear();

            cb_fertig.DataBindings.Add(new Binding("Checked", inliner, "fertig"));
            cbb_wetter.DataBindings.Add(new Binding("Text", inliner, "Wetter"));
            if (inliner.Datum != null)
                dt_eingebaut.DataBindings.Add(new Binding("Value", inliner, "Datum"));
            else
                dt_eingebaut.Enabled = false;
            cb_genehmigung.DataBindings.Add(new Binding("Checked", inliner, "genehmigungVorhanden"));
            cb_stvo.DataBindings.Add(new Binding("Checked", inliner, "sTVOAbsicherung"));
            cb_wasserhaltung.DataBindings.Add(new Binding("Checked", inliner, "wasserhaltungEingerichtet"));
            txt_temp_aussen.DataBindings.Add(new Binding("Text", inliner, "TempAusen"));
            txt_temp_kanal.DataBindings.Add(new Binding("Text", inliner, "TempKanal"));
            
        }

        private void cb_fertig_CheckedChanged(object sender, EventArgs e)
        {
            if(cb_fertig.CheckState == CheckState.Checked)
            {
                if(inliner.Datum == null)
                {
                    inliner.Datum = DateTime.Now;
                    dt_eingebaut.DataBindings.Clear();
                    dt_eingebaut.DataBindings.Add(new Binding("Value", inliner, "Datum"));
                    dt_eingebaut.Enabled = true;

                }
            }
        }

        private void CheckDirectories()
        {
            destinationPath = inliner.CheckVerzeichnisse(Global.Instance.Projektpfad);
        }

        private void btn_get_temp_Click(object sender, EventArgs e)
        {
            ITemperature temperature = TempCAN.TemperaturBuilder.Temperatur(TempCAN.TemperaturSchnittstellen.TINKERFORGE);

            double temperatur = temperature.GetTemperatur();
            inliner.TempAusen = temperatur;
            

            txt_temp_aussen.Update();
        }

        private async void DownloadFromUV()
        {
            Progress<double> progress = new Progress<double>(x =>
            {
                if (x < 0)
                {

                }
                else
                {
                    ftpProgress.Value = Convert.ToInt32(x);
                }
            });
            try
            {

                FtpClient client = new FtpClient("192.168.1.2");
                client.Credentials = new System.Net.NetworkCredential("damian", "bodde05");
                client.Connect();
                //client.ListingParser = FtpParser.UnixAlt;

                ListFiles(client, "/");

                foreach (string file in filenames)
                {
                    string[] _tdateiname = file.Split('/');
                    string dateiname = _tdateiname[_tdateiname.Length - 1];
                    string ordner = _tdateiname[_tdateiname.Length - 2];

                    await client.DownloadFileAsync(Path.Combine(destinationPath, ordner, dateiname), file, true, FluentFTP.FtpVerify.None, progress);
                    client.DeleteFile(file);

                }
            }
            catch (TimeoutException ex)
            {
                MessageBox.Show("Die anlage reagiert nicht!");
            }

        }

        private void btn_transfer_ftp_Click(object sender, EventArgs e)
        {
            filenames.Clear();
            if (MessageBox.Show("Bitte stellen Sie sicher, dass der Server antwortet und dass nur die Dateien vorhanden sind!", "WARNUNG", MessageBoxButtons.OKCancel, MessageBoxIcon.Stop) == DialogResult.OK)
            {
                DownloadFromUV();               
            }
        }

        private void ListFiles(FtpClient client, string directory)
        {
            foreach (FtpListItem item in client.GetListing(directory))
            {
                if (item.Type == FtpFileSystemObjectType.File)
                {
                    filenames.Add(item.FullName);
                }
                else if (item.Type == FtpFileSystemObjectType.Directory)
                {
                    ListFiles(client, item.FullName);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Hashtable grunddaten = new Hashtable()
            {
                {"KL_Wetter","Trocken" }
            };
            List<BilderObject> bilderO = new List<BilderObject>();
            bilderO.Add(new BilderObject()
            {
                ImgID = 1,
                Kommentar = "TestBild",
                Path = @"C:\Users\Damian\Desktop\SanVerwaltung\SanSystem\bin\Debug\projekte\18-850\SW01-SW02\Schachtanbindung\4d0a1627-bd51-48d6-a27e-a4c6691b02d2.jpg"
            });
            BerichtGen.Options options = new BerichtGen.Options("", "", "", grunddaten,bilderO);
            options.ShowDialog();
        }
    }
}
