using DressAtelierContracts.BindingModels;
using DressAtelierContracts.ViewModels;
using DressAtelierDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace DressAtelierFileImplement.Models
{
    public class Employee : IEmployeeModel
    {
        public int ID { get; set; }
        public string FullName { get; set; } = string.Empty;
        public string Password { get; set; } = string.Empty;
        public int WorkExperience { get; set; }
        public int Qualification { get; set; }

        public static Employee? Create(EmployeeBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Employee()
            {
                ID = model.ID,
                FullName = model.FullName,
                WorkExperience = model.WorkExperience,
                Password = model.Password,
                Qualification = model.Qualification
            };
        }

        public static Employee? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new Employee()
            {
                ID = Convert.ToInt32(element.Attribute("ID")!.Value),
                FullName = element.Element("FullName")!.Value,
                WorkExperience = Convert.ToInt32(element.Element("WorkExperience")!.Value),
                Password = element.Element("Password")!.Value,
                Qualification = Convert.ToInt32(element.Element("Qualification")!.Value)
            };
        }

        public void Update(EmployeeBindingModel? model)
        {
            if (model == null) { return; }
            FullName = model.FullName;
            WorkExperience = model.WorkExperience;
            Password = model.Password;
            Qualification = model.Qualification;
        }

        public EmployeeViewModel GetViewModel => new()
        {
            ID = ID,
            FullName = FullName,
            WorkExperience = WorkExperience,
            Password = Password,
            Qualification = Qualification
        };

        public XElement GetXElement => new("Employee", new XAttribute("ID", ID),
                                                       new XElement("FullName", FullName),
                                                       new XElement("Password", Password),
                                                       new XElement("WorkExperience", WorkExperience),
                                                       new XElement("Qualification", Qualification));
    }
}
