using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 ZKuP
{
    /// <summary>
    /// Interaktionslogik für AspInfo.xaml
    /// </summary>
    public partial class AspInfo : MetroWindow
	{
        public int id { get; set; }

        //info = Welcher InfoButton wurde gedrückt?     1 = Ankunft heute; 2 = bereits bestätigt
        public AspInfo(int idFirma_Besucher, string kat, int info = 1, string idzutritte = "1", string Asp = "")
        {
            InitializeComponent();

            if (idzutritte != "1") id = Convert.ToInt32(idzutritte);

            if (info == 1) { lblBemerkung.Visibility = Visibility.Collapsed; tbBemerkung.Visibility = Visibility.Collapsed; btnSignature.Visibility = Visibility.Collapsed; }
            else if (info == 2) { lblBemerkung.Visibility = Visibility.Visible; tbBemerkung.Visibility = Visibility.Visible; }
            else { lblBemerkung.Visibility = Visibility.Visible; tbBemerkung.Visibility = Visibility.Visible; }

            if (kat != "")
            {
                var firmOrVisit = kat == "1" ? "Firmen" : "besucher";
                switch (kat)
                {
                    case "1":
                        firmOrVisit = "Firmen";
                        break;
                    case "2":
                        firmOrVisit = "besucher";
                        break;
                    case "3":
                        firmOrVisit = "family";
                        break;
                }

                tbVorgesetzter.Visibility = Visibility.Visible;
                tbTelVorgesetzter.Visibility = Visibility.Visible;
                lblVorgesetzter.Visibility = Visibility.Visible;
                lblTelVorgesetzter.Visibility = Visibility.Visible;

                lblVertreter_Besucher.Text = "Vertreter";
                lblTelVertreter_Besucher.Text = "Telefonnummer Vertreter";

                string aspName = "";

                if (firmOrVisit != "family")
                    aspName = SQL.ReadSingleValue($"SELECT Ansprechpartner_Intern FROM {MainWindow.table}.{firmOrVisit.ToLower()} WHERE id{firmOrVisit}='{idFirma_Besucher}'");
                else
                    aspName = Asp;

                System.Data.DataTable asp = SQL.ReadSQL($"SELECT * FROM {MainWindow.table}.ansprechpartner WHERE Name='{aspName}'").Result;

                if (asp.Rows.Count > 0)
                {
                    tbAsp.Text = asp.Rows[0].ItemArray[0].ToString();
                    tbVertreter.Text = asp.Rows[0].ItemArray[1].ToString();
                    tbTelAsp.Text = asp.Rows[0].ItemArray[2].ToString();
                    tbVorgesetzter.Text = asp.Rows[0].ItemArray[3].ToString();
                    tbTelVertreter.Text = asp.Rows[0].ItemArray[4].ToString();
                    tbTelVorgesetzter.Text = asp.Rows[0].ItemArray[5].ToString();


                    tbBemerkung.Text = SQL.ReadSingleValue($"SELECT Bemerkung FROM {MainWindow.table}.zutritte WHERE idzutritte='{idzutritte}'");

                    lblParkkarten.Text = SQL.ReadSingleValue($"SELECT GROUP_CONCAT(parkplatznr SEPARATOR ';') FROM {MainWindow.table}.parkplaetze WHERE firma = '{idFirma_Besucher}'").Replace(";", ", ");


                    this.Visibility = Visibility.Visible;
                }
                else
                {
                    MessageBox.Show(this, "Keine oder fehlerhafte Daten angegeben!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                    this.Loaded += (s, e) => this.Close();
                    return;
                }
            }
            else if (kat == "")
            {
                tbVertreter.Visibility = Visibility.Collapsed;
                tbTelVertreter.Visibility = Visibility.Collapsed;
                tbVorgesetzter.Visibility = Visibility.Collapsed;
                tbTelVorgesetzter.Visibility = Visibility.Collapsed;


                lblVertreter_Besucher.Visibility = Visibility.Collapsed;
                lblTelVertreter_Besucher.Visibility = Visibility.Collapsed;
                lblVorgesetzter.Visibility = Visibility.Collapsed;
                lblTelVorgesetzter.Visibility = Visibility.Collapsed;

                btnSignature.Visibility = Visibility.Collapsed;

                lblAsp.Text = "Fahrer:";
                lblTelAsp.Text = "Telefonnummer Fahrer:";
                lblBemerkung.Text = "Parkplatz / Bemerkung";

                System.Data.DataTable asp = SQL.ReadSQL($"SELECT * FROM {MainWindow.table}.zutritte WHERE idzutritte='{idzutritte}'").Result;
                if (asp.Rows.Count > 0)
                {
                    tbAsp.Text = asp.Rows[0].ItemArray[2].ToString().Split(new[] { "Fahrer: " }, StringSplitOptions.None)[1];
                    tbTelAsp.Text = asp.Rows[0].ItemArray[8].ToString();

                    gridInfo.RowDefinitions[4].Height = new GridLength(0);
                    gridInfo.RowDefinitions[5].Height = new GridLength(0);
                    gridInfo.RowDefinitions[6].Height = new GridLength(4, GridUnitType.Star);

                    tbBemerkung.Text = asp.Rows[0].ItemArray[12].ToString();

                    this.Visibility = Visibility.Visible;
                }
            }
            else
            {
                MessageBox.Show(this, "Keine oder fehlerhafte Daten angegeben!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                this.Loaded += (s, e) => this.Close();
                return;
            }
        }

        private async void btnSignature_Click(object sender, RoutedEventArgs e)
        {
            var x = SQL.ReadSingleByteArr($"SELECT signature_blob FROM {MainWindow.table}.zutritte WHERE idzutritte = '{id}'");
            //var i = DBImageManager.ImageFromByte(x);
            ImageView iv;

            if (x != null)
                iv = new ImageView(Helper.ConvertBitmapToImage(x), false);
            else
            {
                MessageBox.Show(this, "Keine Unterschrift vorhanden!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            //iv.BackgroundImage = Helper.ConvertBitmapToImage(x);
            iv.ShowDialog();
        }

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

        private void TelNrs_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            System.Diagnostics.Process.Start($"tel:{(sender as TextBox).Text.Replace(" ", "")}");
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Helper.CheckWindowIsInScreenSpace(this);
        }

        private void Window_LocationChanged(object sender, EventArgs e)
        {
            Helper.CheckWindowIsInScreenSpace(this);
        }

    }
}
