using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
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 mateo
{
    public partial class Profile : Window
    {
        byte[] image;
        public Profile()
        {
            InitializeComponent();
            Update();
        }

        public void Update()
        {
            //фио
            var fio = DB.GetContext().Users.FirstOrDefault(x => x.IDUsers == DB.Iduser);
            name_profile.Text = fio.LastName + " " + fio.FirstName + " " + fio.MiddleName;

            //выводим баланс карты
            DB.Idcard = fio.FKCard;
            balance_profile.Text = String.Format("{0:0.00}", Convert.ToDecimal(DB.GetContext().Card.FirstOrDefault(x => x.IDCard == fio.FKCard).Balance));

            //выводим картинку из бд
            if (fio.Image != null)
            {
                image = fio.Image;
                MemoryStream ms = new MemoryStream(image);
                photo_profile.Source = BitmapFrame.Create(ms);
            }
            if (photo_profile.Source != null) btn_photo.Content = "изменить фото";
        }
        private void btnProfileClick(object sender, RoutedEventArgs e)
        {
        }

        private void btnUslugiClick(object sender, RoutedEventArgs e)
        {
            MainWindow mainWindow = new MainWindow();
            mainWindow.Show();
            this.Close();
        }

        private void btn_balance_Click(object sender, RoutedEventArgs e)
        {
            grid_balance.Visibility = Visibility.Visible;
        }

        private void btn_balanceplus_Click(object sender, RoutedEventArgs e)
        {
            //"пополняем баланс" типа
            if (txt_balanceplus.Text != "")
            {
                var a = DB.GetContext().Card.Where(x => x.IDCard == DB.Idcard).FirstOrDefault();
                a.Balance += Convert.ToDecimal(txt_balanceplus.Text);
                DB.GetContext().SaveChanges();
                grid_balance.Visibility = Visibility.Hidden;
            }
            var fio = DB.GetContext().Users.FirstOrDefault(x => x.IDUsers == DB.Iduser);
            DB.Idcard = fio.FKCard;
            balance_profile.Text = DB.GetContext().Card.FirstOrDefault(x => x.IDCard == fio.FKCard).Balance.ToString();
            Update();
        }

        private void btn_cancelblnc_Click(object sender, RoutedEventArgs e)
        {
            grid_balance.Visibility = Visibility.Hidden;
        }

        private void btn_photo_click(object sender, RoutedEventArgs e)
        {
            try
            {
                string imageLoc;
                OpenFileDialog dld = new OpenFileDialog();
                dld.Filter = "JPG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|JPEG Files (*.jpeg)|*.jpeg";
                dld.Title = "Выберите изображение пользователя";
                bool? result = dld.ShowDialog();
                if(result == true)
                {
                    imageLoc = dld.FileName;
                    photo_profile.Source = new BitmapImage(new Uri(imageLoc));
                    FileStream fs = new FileStream(imageLoc, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);

                    image = br.ReadBytes((int)fs.Length);
                    var img = DB.GetContext().Users.Where(x => x.IDUsers == DB.Iduser).FirstOrDefault();
                    img.Image = image;
                    DB.GetContext().SaveChanges();
                }
                if (photo_profile.Source != null) btn_photo.Content = "изменить фото";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_deletephoto_click(object sender, RoutedEventArgs e)
        {
            var fio = DB.GetContext().Users.FirstOrDefault(x => x.IDUsers == DB.Iduser);
            fio.Image = null;
            DB.GetContext().SaveChanges();
            photo_profile.Source = null;
            btn_photo.Content = "добавить фото";
        }
        #region соцсети
        private void btn_insta(object sender, RoutedEventArgs e)
        {

        }

        private void btn_vk(object sender, RoutedEventArgs e)
        {

        }

        private void btn_whatsapp(object sender, RoutedEventArgs e)
        {

        }

        private void btn_gmail(object sender, RoutedEventArgs e)
        {

        }
        #endregion
        private void btnExit(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void btnLogout(object sender, RoutedEventArgs e)
        {
            First first = new First();
            first.Show();
            this.Close();
        }

        private void btnTickets(object sender, RoutedEventArgs e)
        {

        }
    }
}
