using HorseViewer.Models;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace HorseViewer
{
    public partial class ViewMain : Form
    {
        public ViewMain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*FileDialog fileDialog = new OpenFileDialog();
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                horsePath = fileDialog.FileName;
            }
            var json = File.ReadAllText(horsePath);
            HorseFactory.Horses = JsonSerializer.Deserialize<Dictionary<ulong, HorseViewer.Models.Horse>>(json);

            var result = HorseFactory.GetAllHorses().Values.Select(horse => new HorseGridViewItem
            {
                Id = horse.Id ?? 0,
                Name = horse.HorseName,
                Breed = horse.Breed,
                Gender = horse.Gender,
                Age = horse.Age ?? 0,
                LastDrawnDate = horse.LastDrawnDate,
                IsAllDataLoaded = horse.IsAllDataLoaded()
            }).ToList();

            dataGridView1.DataSource = result;*/
        }

        private void AddNewTab()
        {
            using (var settingsForm = new ViewEditTable())
            {
                if (settingsForm.ShowDialog() == DialogResult.OK)
                {
                    // Erstelle eine neue TabPage
                    var tabPage = new TabPage(settingsForm.TabName);
                    var dataGridView = new DataGridView
                    {
                        Dock = DockStyle.Fill,
                        AutoGenerateColumns = false
                    };

                    // Füge die ausgewählten Properties als Spalten hinzu
                    foreach (var propertyName in settingsForm.SelectedProperties)
                    {
                        dataGridView.Columns.Add(new DataGridViewTextBoxColumn
                        {
                            HeaderText = propertyName,
                            DataPropertyName = propertyName
                        });
                    }

                    // Hole die Daten und filtere nur die ausgewählten Properties
                    var filteredData = HorseFactory.GetAllHorses()
                        .Values
                        .Select(horse => new HorseGridViewItem
                        {
                            Id = horse.Id ?? 0,
                            Name = settingsForm.SelectedProperties.Contains(nameof(HorseGridViewItem.Name)) ? horse.HorseName : null,
                            Breed = settingsForm.SelectedProperties.Contains(nameof(HorseGridViewItem.Breed)) ? horse.Breed : null,
                            Gender = settingsForm.SelectedProperties.Contains(nameof(HorseGridViewItem.Gender)) ? horse.Gender : null,
                            Age = settingsForm.SelectedProperties.Contains(nameof(HorseGridViewItem.Age)) ? horse.Age ?? 0 : 0,
                            LastDrawnDate = settingsForm.SelectedProperties.Contains(nameof(HorseGridViewItem.LastDrawnDate)) ? horse.LastDrawnDate : DateTime.MinValue,
                            IsAllDataLoaded = settingsForm.SelectedProperties.Contains(nameof(HorseGridViewItem.IsAllDataLoaded)) && horse.IsAllDataLoaded()
                        })
                        .ToList();

                    dataGridView.DataSource = filteredData;

                    // Füge DataGridView zur TabPage hinzu
                    tabPage.Controls.Add(dataGridView);

                    // Füge die TabPage zum TabControl hinzu
                    tabControl1.TabPages.Add(tabPage);
                }
            }
        }
        private void ViewMain_Load(object sender, EventArgs e)
        {
            // Initialisiere TabControl mit Standardtab
            var json = File.ReadAllText("Z:\\[01] Kribitz Development\\[02] Projekte\\HR-Collector\\HRServer-Exporter\\HRServer\\Horses.json");
            HorseFactory.Horses = JsonSerializer.Deserialize<Dictionary<ulong, Horse>>(json);
            tabControl1.TabPages.Clear();
            var defaultTab = new TabPage("Default Tab");
            var defaultDataGridView = new DataGridView
            {
                Dock = DockStyle.Fill,
                AutoGenerateColumns = true,
                DataSource = HorseFactory.GetAllHorses().Values.Select(horse => new HorseGridViewItem
                {
                    Id = horse.Id ?? 0,
                    Name = horse.HorseName,
                    Breed = horse.Breed,
                    Gender = horse.Gender,
                    Age = horse.Age ?? 0,
                    LastDrawnDate = horse.LastDrawnDate,
                    IsAllDataLoaded = horse.IsAllDataLoaded()
                }).ToList()
            };
            defaultTab.Controls.Add(defaultDataGridView);
            tabControl1.TabPages.Add(defaultTab);
        }

        private void editTableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddNewTab();
        }
    }
    public class HorseGridViewItem
    {
        public ulong Id { get; set; }
        public string Name { get; set; }
        public string Breed { get; set; }
        public string Gender { get; set; }
        public int Age { get; set; }
        public DateTime LastDrawnDate { get; set; }
        public bool IsAllDataLoaded { get; set; }
    }
}

