﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace PreisAnalyse
{
    public class Produkt : IXmlSerializable
    {
        public string name { get; set; }
        public string amazonId { get; set; }
        public List<Uri> uriList { get; set; }

        public Produkt(string _name, ICollection<Uri> uriCollection, string _amazonId = "")
        {
            name = _name;
            uriList = uriCollection.ToList();
            amazonId = _amazonId;
        }

        public Produkt()
        {
            name = string.Empty;
            amazonId = string.Empty;
            uriList = new List<Uri>();
        }

        public void Replace(Produkt p)
        {
            name = p.name;
            amazonId = p.amazonId;
            uriList = p.uriList;
        }

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            reader.MoveToContent();
            name = reader.GetAttribute("name");
            amazonId = reader.GetAttribute("amazonId");
            if(reader.ReadToDescendant("URI"))
            {
                while(reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "URI")
                {
                    Uri u = new Uri(reader.ReadElementString());
                    uriList.Add(u);
                }
            }
            reader.Read();
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteAttributeString("name", name);
            writer.WriteAttributeString("amazonId", amazonId);
            foreach(Uri u in uriList)
            {
                writer.WriteElementString("URI", u.ToString());              
            }
        }
    }
}
