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.Navigation;
using System.Windows.Shapes;

namespace ViolationNOT
{
    /// <summary>
    /// Interaction logic for NewViolationPage.xaml
    /// </summary>
    public partial class NewViolationPage : Page
    {
        string FilePath;
        public NewViolationPage()
        {
            InitializeComponent();
            DataContext = new Violation();
            CmbType.ItemsSource = DB.db.ViolationType.ToList();
            CmbType.SelectedIndex = 0;
        }

        private void CmbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (CmbType.SelectedIndex == 0)
            {
                TxtCarNumber.Visibility = Visibility.Visible;
            }
            if (CmbType.SelectedIndex == 1)
            {
                TxtCarNumber.Visibility = Visibility.Collapsed;
            }
        }

        private void BtnAddViolation_Click(object sender, RoutedEventArgs e)
        {
            Violation violation = DataContext as Violation;
            if (string.IsNullOrEmpty(violation.Address)||
                string.IsNullOrEmpty(violation.Description))
            {
                Helper.ErroreMessage("Данные не заполнены");
                return;
            }
            if (CmbType.SelectedIndex == 0)
            {
                if (string.IsNullOrEmpty(TxtCarNumber.Text))
                {
                    Helper.ErroreMessage("Номер не введён");
                    return;
                }
                violation.CarNumber = TxtCarNumber.Text;
            }
            violation.ViolationType = CmbType.SelectedItem as ViolationType;
            violation.User = Helper.AuthUser;
            violation.Photo = File.ReadAllBytes(FilePath);
            DB.db.Violation.Add(violation);
            DB.TrySave();
            Helper.InformMessage("Запрос принят");
        }

        private void BtnAddPhoto_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == true)
            {
                FilePath = openFileDialog.FileName;
                Photo.Source = new BitmapImage(new Uri(openFileDialog.FileName));
            }
        }
    }
}
