using LOLAPP.Modele;
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;

namespace LOLAPP.DataContractPersistance
{
    public class DataContract : IPersistanceManager
    {
        public string FilePath2 {get; set;} = Path.Combine(Directory.GetCurrentDirectory(),"//Doss1_XML");
        public string FileName { get; set; } = "Test2.xml";
        public string FilePath { get; set; } = "C:\\Users\\cecharbonn5\\LOLAPP\\LOLAPP";

        public (List<Utilisateur>, List<Champion>) Chargdon()
        {
            var serializer = new DataContractSerializer(typeof(DataToPersist));
            DataToPersist data;
            using (Stream s = File.OpenRead(Path.Combine(FilePath, FileName))) 
            { 
                data = serializer.ReadObject(s) as DataToPersist;
            }
            return (data.util, data.champ);
        }

        public void Sauvdon(List<Utilisateur> s, List<Champion> c)
        {
            var serializer = new DataContractSerializer(typeof(DataToPersist),  
                new DataContractSerializerSettings()
                {
                    PreserveObjectReferences = true
                }
                );
            if (!Directory.Exists(FilePath))
            {
                Debug.WriteLine("Directory crée à l'instant");
                Debug.WriteLine(Directory.GetDirectoryRoot(FileName));
                Debug.WriteLine(FilePath);
                Directory.CreateDirectory(FilePath);
            }

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