using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using WebMVCApi.config;

namespace WebMVCApi.Controllers
{
    [Route("halo/")]
    [ApiController]
    public class DBController : ControllerBase
    {
        [HttpGet("GetHaloContent")]
        public IActionResult GetHaloContent()
        {
           var list = new ArrayList();
            var dbx =  new HaloDbContext();
            var conn =  dbx.Database.GetDbConnection();
            if(conn.State != System.Data.ConnectionState.Open)
            {
                conn.Open();
            }
            using (var cmd = conn.CreateCommand())
            {
               // cmd.CommandText = "SELECT\r\n\tt1.id,\r\n\tIFNULL(GROUP_CONCAT( t5.NAME ) , '')AS cates ,\r\n\tIFNULL(GROUP_CONCAT( t6.NAME ) , '')AS tags ,\r\n\tt2.original_content,\r\n\tt1.title\r\nFROM\r\n  contents t2 \r\n\tLEFT JOIN posts t1\r\n\tON t1.id =  t2.post_id\r\n\t\r\n  LEFT JOIN post_categories t3 \r\n\tON t1.id =  t3.post_id\r\n\t\r\n\tLEFT JOIN post_tags t4 \r\n\tON t1.id =  t4.post_id\r\n\t\r\n\tLEFT JOIN categories t5\r\n\tON t3.category_id =  t5.id\r\n\t\r\n\tLEFT JOIN tags t6\r\n\tON t4.tag_id =  t6.id\r\nGROUP BY\r\n\tt2.post_id";
                cmd.CommandText = "SELECT\r\n\tt1.id,\r\n\tIFNULL(GROUP_CONCAT( t5.NAME ) , '')AS cates ,\r\n\tIFNULL(GROUP_CONCAT( t6.NAME ) , '')AS tags ,\r\n\tt2.original_content,\r\n\tt1.title,\r\n\tt1.create_time\r\nFROM\r\n  contents t2 \r\n\tLEFT JOIN posts t1\r\n\tON t1.id =  t2.post_id\r\n\t\r\n  LEFT JOIN post_categories t3 \r\n\tON t1.id =  t3.post_id\r\n\t\r\n\tLEFT JOIN post_tags t4 \r\n\tON t1.id =  t4.post_id\r\n\t\r\n\tLEFT JOIN categories t5\r\n\tON t3.category_id =  t5.id\r\n\t\r\n\tLEFT JOIN tags t6\r\n\tON t4.tag_id =  t6.id\r\nGROUP BY\r\n\tt2.post_id";
                var read = cmd.ExecuteReader();
                while (read.Read()) { 
                    var id  = read.GetInt32(0);
                    var cates  = read.GetString(1);
                    var tags  = read.GetString(2);
                    var content  = read.GetString(3);
                    var title  = read.GetString(4);
                    var createTime  = read.GetDateTime(5);
                    var setCates = cates.Split(',').ToHashSet();
                    var setTags = tags.Split(',').ToHashSet();
                    string head =
                        $"---\r\n" +
                        $"icon: edit\r\n" +
                        $"date: {createTime.ToString("yyyy-MM-dd")}\r\n";
                        
                        if (setCates.First()!="")
                        {
                            head += $"category:\r\n";
                        }
                       
                        foreach (var cate in setCates)
                        {
                            if (!cate.Equals("")) {
                            head += $"  - {cate}\r\n";
                            }                      
                        }
                        if (setTags.First() != "")
                        {
                            head += $"tag:\r\n";
                        }
                    
                        foreach (var tag in setTags)
                        {
                            if (!tag.Equals("")) { 
                                head += $"  - {tag}\r\n";
                            }
                        }
                    head +=
                    "headerDepth: 5\r\n" +
                    "---\r\n\n\n" +
                    $"# {title}\r\n";
                    title = title.Replace("/", "");
                    System.IO.File.WriteAllText($"./md/{title}.md", head+ content);
                    Console.WriteLine(id);
                    list.Add(content);
                }
            }
            return Ok(list);
        }
      
    }

  
}
