using App.Module.Sqler.Logical.SqlVersion;

using Microsoft.AspNetCore.Mvc;

using Sqler.Module.Sqler.Logical.Message;

namespace App.Module.Sqler.Controllers.SqlVersion
{
    [Route("sqler/SqlVersion")]
    [ApiController]
    public class SqlVersionController : ControllerBase
    {


        #region Util
        void SendMsg(EMsgType type, String msg)
        {
            MessageHelp.SendMsg(Response, type, msg, false);
        }
        #endregion


        #region 升级

        /// <summary>
        /// 升级
        /// </summary>
        /// <returns></returns>
        [HttpGet("upgrade")]
        public void Upgrade([FromQuery] string template, [FromQuery] int version)
        {

            Response.ContentType = "text/html;charset=utf-8";

            SqlVersionService.UpgradeToVersion(template, SendMsg, version);
        }



        /// <summary>
        /// 一键升级
        /// </summary>
        /// <returns></returns>
        [HttpGet("oneKeyUpgrade")]
        public void OneKeyUpgrade()
        {

            Response.ContentType = "text/html;charset=utf-8";


            foreach (var dataProvider in SqlVersionHelp.sqlCode_DataProviders)
            {
                SqlVersionService.UpgradeToVersion(dataProvider.template, SendMsg);
            }
        }

        #endregion






        #region DownloadSql
        /// <summary>
        /// 获取模板列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("download")]
        public void DownloadSql([FromQuery] string module)
        {

            Response.ContentType = "text/file;charset=utf-8";
            //Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(responseFileName, Encoding.UTF8));
            Response.Headers.Add("Content-Disposition", "attachment;filename=SqlVersion_" + module + "(" + DateTime.Now.ToString("yyyy-MM-dd_HHmmss") + ").sql");

            void sendMsg(String msg)
            {
                Response.WriteAsync(msg + "\r\n");
            }

            var query = SqlVersionHelp.sqlCode_DataProviders.AsQueryable();

            if (!string.IsNullOrEmpty(module))
            {
                query = query.Where(m => m.module == module);
            }

            var dataProviders = query.ToList();

            foreach (var repository in dataProviders)
            {
                sendMsg("---------------------------------------------------------------------------");

                sendMsg("-- module: " + repository.module);

                var codes = repository.GetList().items.OrderBy(m => m.version);
                if (codes == null) continue;

                #region foreach

                foreach (var code in codes)
                {
                    sendMsg("---------------------------------");
                    sendMsg("-- version: " + code.version + "  time:" + code.time);
                    if (!String.IsNullOrWhiteSpace(code.comment))
                    {
                        sendMsg("\r\n/* ");
                        sendMsg(code.comment);
                        sendMsg("*/\r\n");
                    }
                    sendMsg(code.code?.Replace("\n", "\r\n"));
                }
                #endregion
            }
        }
        #endregion



    }
}
