using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BeWo
{
    /// <summary>
    /// Interaktionslogik für ProxyLoginView.xaml
    /// </summary>
    public partial class ProxyLoginView : Window
    {
        public ProxyLoginView()
        {
            InitializeComponent();


            überprüfe();
        }


        private void überprüfe() {

            if (proxynametxt.Text.Equals("") && proxybenutzer.Text.Equals("") && proxypasswort.Password.Equals("")) {

                int counter = 0;
                string line;

                string[] datai= new string[3];

                try
                {
					var filePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\proxy.dat";
					if (!File.Exists(filePath))
					{
						filePath = System.IO.Path.Combine(BeWoApp.GetAndCreateUserAppDataPath(), "proxy.dat");
					}

                    var file = new System.IO.StreamReader(filePath);
                    while ((line = file.ReadLine()) != null)
                    {

                        byte[] decbuff = Convert.FromBase64String(line);
                        var x= System.Text.Encoding.UTF8.GetString(decbuff);
                        datai[counter] = x;

                        counter++;
                    }

                    file.Close();

                    proxynametxt.Text = datai[0];
                    proxybenutzer.Text = datai[1];
					proxypasswort.Password = datai[2];
                }catch(IOException i){
                    //Fehlermeldung
                }
            }
        
        }

        private void proxyOK_Click(object sender, RoutedEventArgs e)
        {
            string ptname = proxynametxt.Text;
            string ptbenutzer = proxybenutzer.Text;
			string ptpasswd = proxypasswort.Password;

            string[] lines = { ptname, ptbenutzer, ptpasswd };

            try
            {
				var filePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\proxy.dat";
				if (!File.Exists(filePath))
				{
					filePath = System.IO.Path.Combine(BeWoApp.GetAndCreateUserAppDataPath(), "proxy.dat");
				}

                using (System.IO.StreamWriter file =
				new System.IO.StreamWriter(filePath))
                {
                    foreach (string line in lines)
                    {
                        byte[] encData_byte = new byte[line.Length];
                        encData_byte = System.Text.Encoding.UTF8.GetBytes(line);
                        string encodedData = Convert.ToBase64String(encData_byte);

                        file.WriteLine(encodedData);
                    }
                }

                ptname = "";
                ptbenutzer = "";
                ptpasswd = "";

                this.Close();

            }catch(IOException i){
                //Fehlermeldung
            }
        }


        private void proxyAbbr_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
