using Microsoft.Win32;
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 Demoexample
{
    /// <summary>
    /// Логика взаимодействия для Redaction.xaml
    /// </summary>
    public partial class Redaction : Window
    {
        string imgg = "";
        public Agent buffer;
        public Agent changeagent;
        ViewModel viewModel = new ViewModel();
        public Redaction(Agent idagent)
        {
            InitializeComponent();
            DataContext = idagent;
            buffer = idagent;
            changeagent = idagent;
            Type.ItemsSource = DataBase.BaseModel.AgentType.ToList();
            Type.SelectedValuePath = "ID";
            Type.DisplayMemberPath = "Title";
            Type.SelectedIndex = (int)buffer.AgentTypeID;
        }

        private void add_Click(object sender, RoutedEventArgs e)
        {
            List<Agent> pro = DataBase.BaseModel.Agent.ToList();
            if (Convert.ToInt32(priority.Text) < 0 && Convert.ToInt32(priority.Text) % 10 == 0)
            {
                throw new Exception("Приоритет не может быть отрицательным");
            }
            changeagent.Title = name.Text;
            changeagent.AgentTypeID = Type.SelectedIndex;
            changeagent.Priority = Convert.ToInt32(priority.Text);
            changeagent.Logo = imgg;
            changeagent.Address = address.Text;
            changeagent.INN = inn.Text;
            changeagent.KPP = kpp.Text;
            changeagent.DirectorName = director.Text;
            changeagent.Phone = telephone.Text;
            changeagent.Email = email.Text;

            DataBase.BaseModel.SaveChanges();
            viewModel.agents = viewModel.newagent();

            new MainWindow().Show();
            this.Close();
        }

        private void newImg_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.DefaultExt = ".jpg";
            openFileDialog.Filter = "Изображение | *.jpg; *.png";
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                imgg = openFileDialog.FileName;
                imgnew.Source = BitmapFrame.Create(new Uri(imgg));
            }
        }

        private void delit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Agent agent = DataBase.BaseModel.Agent.FirstOrDefault(x => x.ID == buffer.ID);
                DataBase.BaseModel.Agent.Remove(agent);
                DataBase.BaseModel.SaveChanges();
                ViewModel viewModel = new ViewModel();
                new MainWindow().Show();
                this.Close();
            }
            catch
            {
                MessageBox.Show("Запись не может быть удалена из-за связей");
            }
        }
    }
}
