using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace AutoUpdate
{
  public class ClientVersion
  {
    public string Version { get; set; }

    public string ServerUrl { get; set; }

    private List<UpdateFile> _update = new List<UpdateFile>();
    public List<UpdateFile> UpdateFiles { get { return _update; } set { _update = value; } }
  }

  public class UpdateFile
  {
    public string FileName { get; set; }

    public string Url { get; set; }

    public int Size { get; set; }

    public bool NeedRestart { get; set; }
  }

  public static class XmlUtil
  {
    public static T XmlDeserializeObject<T>(string xmlOfObject) where T : class
    {
      using (MemoryStream ms = new MemoryStream())
      {
        using (StreamWriter sr = new StreamWriter(ms, Encoding.UTF8))
        {
          sr.Write(xmlOfObject);
          sr.Flush();
          ms.Seek(0, SeekOrigin.Begin);
          XmlSerializer serializer = new XmlSerializer(typeof(T));
          return serializer.Deserialize(ms) as T;
        }
      }
    }

    public static T DeserializeFromFile<T>(string fileName = "")
    {
      if (string.IsNullOrWhiteSpace(fileName))
      {
        fileName = typeof(T).Name + ".xml";
      }
      if (!File.Exists(fileName))
      {
        throw new FileNotFoundException("不存在文件：" + fileName);
      }
      using (var reader = new StreamReader(fileName))
      {
        var xs = new XmlSerializer(typeof(T));
        object obj = xs.Deserialize(reader);
        reader.Close();
        return (T)obj;
      }
    }
  }
}
