using ProjectManagementSystem.Common.Config;
using ProjectManagementSystem.WebApi.Filters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;

namespace ProjectManagementSystem.WebApi.Controller
{
    [GlobalActionFilter]
    [ApiExplorerSettings(IgnoreApi = true)]
    /// <summary>
    /// 版本信息和提供自动更新下载
    /// </summary>
    public class VerController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage PmsXml()
        {
            try
            {
                string currPath = ExcelConfig.Instance.CurrentPath;
                string dataFile = Path.Combine(currPath, "pms.xml");

                string reuslt = File.ReadAllText(dataFile, System.Text.Encoding.UTF8);
                var response = new HttpResponseMessage()
                {
                    Content = new StringContent(reuslt, System.Text.Encoding.UTF8, "text/xml")
                };
                return response;
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message);
            }
        }

        [HttpGet]
        public HttpResponseMessage PmsZip()
        {
            try
            {
                string currPath = ExcelConfig.Instance.CurrentPath;
                string dataFile = Path.Combine(currPath, "pms.zip");

                var stream = new FileStream(dataFile, FileMode.Open);
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StreamContent(stream);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "pms.zip"
                };
                return response;
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message);
            }
        }
    }
}
