using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.Logging;
using ReportService.Database;

namespace ReportService.Controllers.api
{
    [Area("api")]
    [Route("api/[Controller]/[Action]")]
    public class ReportApiController:Controller
    {
        public IWebHostEnvironment Env { get; private set; }
        public RSDbContext Db { get; set; }
        public ILogger Logger { get; set; }

        /// <summary>
        /// 报表存放位置
        /// </summary>
        public string ReportPath {
            get {
                return Path.Combine(this.Env.ContentRootPath,"wwwroot/report");
            }
        }

        public ReportApiController(ILogger<ReportApiController> logger,IWebHostEnvironment env,RSDbContext db) {
            this.Logger = logger;
            this.Env = env;
            this.Db = db;
        }

        /// <summary>
        /// 获取页面模板列表
        /// </summary>
        /// <returns>名称列表</returns>
        public List<string> GetReportFiles() {
            var files = Directory.GetFiles(this.ReportPath,"*.html");
            var result = new List<string>();
            foreach(var f in files) {
                var file = new System.IO.FileInfo(f);
                if(file.Name.ToLower().EndsWith(".rpt.html")) {
                    continue;
                }
                result.Add(file.Name.Substring(0,file.Name.IndexOf('.')));
            }
            this.Logger.LogInformation($"GetReportFiles:{string.Join(',',result.ToArray())}");
            return result;
        }
        /// <summary>
        /// 获取模板内容显示模板页面
        /// </summary>
        /// <param name="fileName">模板文件名</param>
        /// <returns>模板内容</returns>
        public IActionResult GetHtml(string fileName) {
            var htmlFile = Path.Combine(this.ReportPath,fileName + ".html");
            using(var fr = System.IO.File.OpenRead(htmlFile)) {
                using(var sr = new StreamReader(fr)) {
                    this.Logger.LogInformation($"GetHtml:{fileName}");
                    return Content(sr.ReadToEnd());
                }
            }
        }

        /// <summary>
        /// 获取模板内容显示模板页面
        /// </summary>
        /// <param name="fileName">模板文件名</param>
        /// <returns>模板内容</returns>
        public IActionResult GetPrint(string fileName) {
            var htmlFile = Path.Combine(this.ReportPath,fileName + ".rpt.html");
            using(var fr = System.IO.File.OpenRead(htmlFile)) {
                using(var sr = new StreamReader(fr)) {
                    this.Logger.LogInformation($"GetPrint:{fileName}");
                    return Content(sr.ReadToEnd(),"text/html; charset=utf-8");
                }
            }
        }

        /// <summary>
        /// 调用database执行sql语句，将结果封装为json对象返回
        /// </summary>
        /// <param name="sql">要执行的sql语句</param>
        /// <returns>json对象</returns>
        public object GetResult(string sql) {
            var result = this.Db.SqlJsonQuery(sql);
            this.Logger.LogInformation($"GetResult:\n{sql}\n{result}");
            return Content(result,"application/json; charset=utf-8");
        }

    }
}
