using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using demofinish.Models;

namespace demofinish
{
    public partial class MainWindow : Window
    {
        private ObservableCollection<Agent> agents = new ObservableCollection<Agent>();
        private List<Agent> agentsList = new List<Agent>();

        public MainWindow()
        {
            InitializeComponent();
            LoadAgents();
        }

        private Bitmap? GetImage(string logo)
        {
            try
            {
                string absolutePath = Path.Combine(AppContext.BaseDirectory, logo);
                return new Bitmap(absolutePath);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Ошибка загрузки изображения: {ex.Message}");
                return null;
            }
        }
        


        private void LoadAgents()
        {
            using var context = new User1Context();


            agentsList = context.Agents.ToList();


            foreach (var agent in agentsList)
            {
                agent.Image = GetImage(agent.Logo);
            }

            agents.Clear();
            foreach (var agent in agentsList)
            {
                agents.Add(agent);
            }

            AgentListBox.ItemsSource = agents;
        }
    }
}