using System;
using System.Collections.Generic;
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;
using System.Xml.Linq;
using System.Xml;
using System.ComponentModel.DataAnnotations;
using Telerik.Windows.Controls;
using System.IO;
using Newtonsoft.Json;

namespace TestApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            StyleManager.ApplicationTheme = new VisualStudio2013Theme();

            InitializeComponent();

            string appPath = AppDomain.CurrentDomain.BaseDirectory;
            string JsonFilePath = System.IO.Path.Combine(appPath, "..\\..\\Fiches.json");

            ReadJsonFile(JsonFilePath);
        }

        private void ReadJsonFile(string jsonFilePath)
        {
            try
            {
                if (File.Exists(jsonFilePath))
                {
                    string jsonString = File.ReadAllText(jsonFilePath);

                    var pagedFicheRList = JsonConvert.DeserializeObject<PagedSearchFormList>(jsonString);

                    DataGridView.ItemsSource = pagedFicheRList.Resources;
                }
                else
                {
                    throw new Exception($"Le fichier {jsonFilePath} n'existe pas.");
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"Une erreur s'est produite : {ex.Message}");
            }
        }

        private void DataGridView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.UniqueName == "Contacts" || e.Column.UniqueName == "Timetables")
            {
                e.Cancel = true;
            }
        }
    }

    public partial class Contact
    {

        public Contact()
        {
            OnCreated();
        }

        public System.Guid Id { get; set; }

        public string Name { get; set; }

        public string Firstname { get; set; }

        public string Email { get; set; }

        public string Phone { get; set; }

        public virtual SearchForm SearchForm { get; set; }

        #region Extensibility Method Definitions

        partial void OnCreated();

        #endregion
    }

    public class PagedSearchFormList
    {
        public PagedSearchFormList()
        {
            Resources = new List<SearchForm>();
        }
        public List<SearchForm> Resources { get; set; }
        public int TotalCount { get; set; }
        public int PageCount { get; set; }
        public int PageSize { get; set; }
        public int CurrentPage { get; set; }
    }
    public partial class SearchForm
    {

        public SearchForm()
        {
            this.Contacts = new List<Contact>();
            this.Timetables = new List<Timetable>();
            OnCreated();
        }

        [Display(AutoGenerateField = false)]
        public System.Guid Id { get; set; }

        [Display(Name = "Repère", Order = 0)]
        public string Repere { get; set; }

        [Display(Name = "Equipement")]
        public string EquipementActivite { get; set; }

        [Display(Name = "Asservissement")]
        public string Asservissement { get; set; }

        public DateTime? DateModification { get; set; }

        [Display(Name = "Isolée", Order = 2)]
        public bool Isolee { get; set; }
        public virtual IList<Contact> Contacts { get; set; }
        public virtual IList<Timetable> Timetables { get; set; }

        #region Extensibility Method Definitions

        partial void OnCreated();

        #endregion
    }

    public partial class Timetable
    {

        public Timetable()
        {
            OnCreated();
        }

        public System.Guid Id { get; set; }

        public DateTime? StartDate { get; set; }

        public TimeSpan? StartHour { get; set; }

        public DateTime? EndDate { get; set; }

        public TimeSpan? EndHour { get; set; }

        public string Periodicity { get; set; }

        public virtual SearchForm SearchForm { get; set; }

        #region Extensibility Method Definitions

        partial void OnCreated();

        #endregion
    }

}
