using combit.Reporting;
using combit.Reporting.DataProviders;
using ListLabelObjects.Models;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

namespace ListLabelObjects.ViewModels
{
    public class MainWindowViewModel
    {
        public ICommand DesignButtonClicked { get; }
        public ICommand ShowButtonClicked { get; }

        public MainWindowViewModel()
        {
            DesignButtonClicked = new RelayCommand(DoDesignReport);
            ShowButtonClicked = new RelayCommand(DoShowReport);
        }

        private void DoShowReport(object parameter)
        {
            var personen = GetPersonen();

            var listLabel = new ListLabel();
            try
            {
                listLabel.Core.LlSetOptionString(LlOptionString.DefaultDesignScheme, "COMBITCOLORWHEEL");

                listLabel.DataSource = new ObjectDataProvider(personen);
                listLabel.AutoProjectFile = "persons.lst";
                listLabel.AutoShowSelectFile = true;

                listLabel.AutoDestination = LlPrintMode.Preview;

                listLabel.AutoShowPrintOptions = false;
                listLabel.DrilldownAvailable = true;

                listLabel.AutoProjectType = LlProject.List;

                listLabel.Print();

            }
            catch (LL_User_Aborted_Exception)
            {
            }
            catch (ListLabelException llException)
            {
                // Catch Exceptions
                MessageBox.Show($"Information: {llException.Message}\n\nThis information was generated by a List & Label custom exception.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            finally
            {
                listLabel.Dispose();
            }
        }

        private void DoDesignReport(object parameter)
        {
            var personen = GetPersonen();

            var listLabel = new ListLabel();
            try
            {
                listLabel.Core.LlSetOptionString(LlOptionString.DefaultDesignScheme, "COMBITCOLORWHEEL");

                listLabel.DataSource = new ObjectDataProvider(personen);

                listLabel.AutoShowSelectFile = true;

                listLabel.DrilldownAvailable = true;

                listLabel.AutoDestination = LlPrintMode.PreviewControl;

                listLabel.AutoProjectFile = "persons.lst";
                listLabel.AutoProjectType = LlProject.List | LlProject.FileAlsoNew;
                listLabel.AutoShowPrintOptions = false;

                listLabel.Design();

            }
            catch (LL_User_Aborted_Exception)
            {
            }
            catch (ListLabelException llException)
            {
                // Catch Exceptions
                MessageBox.Show($"Information: {llException.Message}\n\nThis information was generated by a List & Label custom exception.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            finally
            {
                listLabel.Dispose();
            }
        }

        private static List<Person> GetPersonen()
        {
            var result = new List<Person>();

            var person1 = new Person
            {
                FirstName = "John", LastName = "Doe",
                Projects = new List<Project>
                {
                    new Project
                    {
                        ProjectName = "Project A", ProjectNumber = "4711",
                        WorkingHours = new List<WorkingHour>()
                        {
                            new WorkingHour()
                                { Duration = 8.0, Date = new DateTime(2026, 3, 28), Comment = "Viel getan John Doe" },
                            new WorkingHour()
                                { Duration = 8.0, Date = new DateTime(2026, 3, 28), Comment = "Viel getan John Doe" },
                            new WorkingHour()
                            {
                                Duration = 8.0, Date = new DateTime(2026, 3, 29), Comment = "Viel getan am 29 John Doe"
                            },
                            new WorkingHour()
                            {
                                Duration = 8.0, Date = new DateTime(2026, 3, 29), Comment = "Viel getan am 29. John Doe"
                            },
                            new WorkingHour()
                            {
                                Duration = 8.0, Date = new DateTime(2026, 3, 30), Comment = "Viel getan am 30. John Doe"
                            }
                        }
                    },
                    new Project
                    {
                        ProjectName = "Project B", ProjectNumber = "0815",
                        WorkingHours = new List<WorkingHour>()
                        {
                            new WorkingHour()
                            {
                                Duration = 8.0, Date = new DateTime(2026, 3, 29), Comment = "Viel getan am 29 John Doe"
                            },
                            new WorkingHour()
                                { Duration = 2.0, Date = new DateTime(2026, 3, 28), Comment = "Anreise John Doe" }
                        }
                    }
                }
            };
            result.Add(person1);

            var person2 = new Person
            {
                FirstName = "Peter, Paul", LastName = "Mary",
                Projects = new List<Project>
                {
                    new Project
                    {
                        ProjectName = "Project A", ProjectNumber = "4711",
                        WorkingHours = new List<WorkingHour>()
                        {
                            new WorkingHour()
                                { Duration = 2.0, Date = new DateTime(2026, 3, 28), Comment = "Viel getan Mary" },
                            new WorkingHour()
                                { Duration = 4.0, Date = new DateTime(2026, 3, 28), Comment = "Viel getan Mary" },
                            new WorkingHour()
                                { Duration = 5.0, Date = new DateTime(2026, 3, 29), Comment = "Viel getan am 29 Mary" },
                            new WorkingHour()
                            {
                                Duration = 1.0, Date = new DateTime(2026, 3, 29), Comment = "Viel getan am 29. Mary"
                            },
                            new WorkingHour()
                                { Duration = 2.0, Date = new DateTime(2026, 3, 30), Comment = "Viel getan am 30. Mary" }
                        }
                    },
                    new Project
                    {
                        ProjectName = "Project B", ProjectNumber = "0815",
                        WorkingHours = new List<WorkingHour>()
                        {
                            new WorkingHour()
                                { Duration = 6.0, Date = new DateTime(2026, 3, 29), Comment = "Viel getan am 29 Mary" },
                            new WorkingHour()
                                { Duration = 4.0, Date = new DateTime(2026, 3, 28), Comment = "Anreise Mary" }
                        }
                    },
                    new Project
                    {
                        ProjectName = "Project C", ProjectNumber = "0042",
                        WorkingHours = new List<WorkingHour>()
                        {
                            new WorkingHour()
                                { Duration = 1.0, Date = new DateTime(2026, 3, 29), Comment = "Viel getan am 29 Mary" },
                            new WorkingHour()
                                { Duration = 1.0, Date = new DateTime(2026, 3, 28), Comment = "Anreise Mary" }
                        }
                    }
                }
            };
            result.Add(person2);


            return result;
        }
    }
}