using CanYouBuildIt.DataContractPersistance;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CanYouBuildIt.Model
{
    public class Manager
    {
        public List<Utilisateur> listUtil { get; private set; }
        public List<Composant> listComp { get; private set; }
        public List<Build> listBuild { get; private set; }
        public IPersistanceManager Persi { get; set; }

        public Manager()
        {
            listUtil = new List<Utilisateur>();
            listComp = new List<Composant>();
            listBuild = new List<Build>();
        }
        public Manager(IPersistanceManager pers) 
        {
            listUtil = new List<Utilisateur>();
            listComp = new List<Composant>();
            listBuild = new List<Build>();
            Persi = pers;
        }

        public void chargeDonne()
        {
            DataToPersist donnee = Persi.chargeDonnee();
            listUtil.AddRange(donnee.lu);
            listComp.AddRange(donnee.lc);
            listBuild.AddRange(donnee.lb);
        }

        public void sauvegardeDonnee()
        {
            DataToPersist data = new DataToPersist();
            data.lu.AddRange(listUtil);
            data.lc.AddRange(listComp);
            data.lb.AddRange(listBuild);
            Persi.sauvegardeDonnee(data);
        }

        public void ajouterUtilisateur(Utilisateur utilisateur)
        {
            listUtil.Add(utilisateur);
        }

        public void ajouterComposant(Composant composant)
        {
            listComp.Add(composant);
        }

        public void ajouterBuild(Build build)
        {
            listBuild.Add(build);
        }

        public int rechercheUsername(string usern)
        {
            for (int i = 0; i < listUtil.Count; i++ )
            {
                if (listUtil[i].username.Equals(usern,StringComparison.OrdinalIgnoreCase)){
                    return i;
                }
            }
            return -1;
        }

        public int recherchePwd(string pwd)
        {
            for (int i = 0; i < listUtil.Count; i++)
            {
                if (listUtil[i].password.Equals(pwd,StringComparison.OrdinalIgnoreCase))
                {
                    return i;
                }
            }
            return -1;
        }
    }
}
