using GameAtlas.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
using System.Collections.ObjectModel;

namespace GameAtlas.DataContractPersistance
{
    public class DataContractPers : IPersistanceManager 
    {
        public string FileName { get; set; } = "data.xml";

        public string FilePath { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory); //Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/data";

        public (ObservableCollection<Jeu>, List<Admin>, List<Utilisateur>) ChargeDonnees()
        {
            var serializer = new DataContractSerializer(typeof(DataToPersist));
            DataToPersist data = new DataToPersist();

            using (Stream s = File.OpenRead(Path.Combine(FilePath, FileName)))
            {
                data = serializer.ReadObject(s) as DataToPersist;
            }
            return (data.jeux, data.admins, data.utilisateurs);
        }

        public void SauvegardeDonnees(ObservableCollection<Jeu> jeux, List<Admin> admins, List<Utilisateur> users)
        {
            var serializer = new DataContractSerializer(typeof(DataToPersist));

            if(!Directory.Exists(FilePath))
            {
                Debug.WriteLine("Directory créé");
                Debug.WriteLine(Directory.GetDirectoryRoot(FilePath));
                Debug.WriteLine(FilePath);
                Directory.CreateDirectory(FilePath);
            }

            DataToPersist data = new DataToPersist();
            data.jeux = jeux;
            data.admins = admins;
            data.utilisateurs = users;

            var settings = new XmlWriterSettings() { Indent = true };
            using (TextWriter tw = File.CreateText(Path.Combine(FilePath, FileName)))
            {
                using (XmlWriter writer = XmlWriter.Create(tw, settings))
                {
                    serializer.WriteObject(writer, data);
                }
            }

            /*using (Stream s = File.Create(Path.Combine(FilePath, FileName)))
            {
                serializer.WriteObject(s, users);
            }*/
        }

        public void SauvegardeUser(Utilisateur user)
        {
            var serializer = new DataContractSerializer(typeof(List<Utilisateur>));

            if (!Directory.Exists(FilePath))
            {
                Debug.WriteLine("Directory créé");
                Debug.WriteLine(FilePath);
                Directory.CreateDirectory(FilePath);
            }

            var settings = new XmlWriterSettings() { Indent = true };
            using (TextWriter tw = File.CreateText(Path.Combine(FilePath, FileName)))
            {
                using (XmlWriter writer = XmlWriter.Create(tw, settings))
                {
                    serializer.WriteObject(writer, new List<Utilisateur> { user });
                }
            }

            Debug.WriteLine("Un utilisateur a été sauvegardé !");
        }


        /*public (List<Jeu>, List<Utilisateur>) ChargeDonnees2()
        {
            var serializer = new DataContractSerializer(typeof(List<Utilisateur>));

            DataToPersist data;
            using (Stream s = File.OpenRead(Path.Combine(FilePath, FileName)))
            {
                data = serializer.ReadObject(s) as DataToPersist;
            }
            return (data.jeux, data.users);
        }*/


    }
}
