using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using DemoService.Context;
using DemoService.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Net;

namespace DemoService;

public partial class AddEditWindow : Window
{
    private bool AdminMode;
    private int serviceID;
    public AddEditWindow()
    {
        InitializeComponent();
        Icon = new WindowIcon(new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "/Assets/service_logo.ico"));
    }
    public AddEditWindow(bool AdminMode)
    {
        InitializeComponent();
        this.AdminMode = AdminMode;
        ServiceIDTextBlock.IsVisible = false;
        EditServiceButton.IsVisible = false;
        Icon = new WindowIcon(new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "/Assets/service_logo.ico"));
    }
    public AddEditWindow(bool AdminMode, int ID)
    {
        InitializeComponent();
        this.AdminMode = AdminMode;
        serviceID = ID;
        ServiceID.Text = serviceID.ToString();
        using (var context = new OvsyannikovContext())
        {
            var service = context.Services.Find(serviceID);
            ServiceName.Text = service.Title;
            Cost.Text = Convert.ToString(service.Cost);
            DurationInSeconds.Text = Convert.ToString(service.DurationInSeconds);
            Description.Text = service.Description;
            Discount.Text = Convert.ToString(service.Discount);
            ServiceImage.Source = service.MainImagePathBitmap;
        }
        AddServiceButton.IsVisible = false;
        Icon = new WindowIcon(new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "/Assets/service_logo.ico"));
    }

    private async void AddPictureButton_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        /*OpenFileDialog openFileDialog = new OpenFileDialog();
        var topLevel = await openFileDialog.ShowAsync(this);
        */
    }

    private void AddServiceButton_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        Service service = new Service()
        {
            Id = Utils.Context.Services.Count + 1,
            Title = ServiceName.Text,
            Cost = Convert.ToDecimal(Cost.Text),
            DurationInSeconds = Convert.ToInt32(DurationInSeconds.Text),
            Description = Description.Text,
            Discount = Convert.ToDecimal(Discount.Text)
        };
        using (var context = new OvsyannikovContext())
        {
            context.Add(service);
            context.SaveChanges();
        }
        Utils.Context.Services = new List<Service>(Utils.Context.DbContext.Services);
        ServiceWindow serviceWindow = new(AdminMode);
        serviceWindow.Show();
        Close();
    }

    private void EditServiceButton_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        Service service = new Service()
        {
            Id = serviceID,
            Title = ServiceName.Text,
            Cost = Convert.ToDecimal(Cost.Text),
            DurationInSeconds = Convert.ToInt32(DurationInSeconds.Text),
            Description = Description.Text,
            Discount = Convert.ToDecimal(Discount.Text)
        };
        using (var context = new OvsyannikovContext())
        {
            var oldService = context.Services.Find(serviceID);
            oldService = service;
            context.SaveChanges();
        }
        Utils.Context.Services = new List<Service>(Utils.Context.DbContext.Services);
        ServiceWindow serviceWindow = new(AdminMode);
        serviceWindow.Show();
        Close();
    }
}