using System.Collections.Generic;
using FreeSql;
using JXCMS.CMS.Admin.Models;
using JXCMS.CMS.Entity;
using JXCMS.CMS.Movie.Entity;
using JXCMS.Core.Db;
using JXCMS.Core.Encrypt;
using Microsoft.AspNetCore.Mvc;

namespace JXCMS.CMS.Admin.Controllers
{
    [Area("Admin")]
    public class InstallController : Controller
    {
        // GET
        public IActionResult Index()
        {
            if (System.IO.File.Exists("install.lock"))
            {
                return RedirectToAction("Index", "Home", new {area = ""});
            }
            return View();
        }

        public JsonResult CheckInstallInfo(InstallModel installModel)
        {
            var ret = DbExtension.SetDb(installModel.ToDbConfig(), true);
            return new JsonResult(new {ret = ret.isSuccess, msg = ret.msg});
        }

        public IActionResult Finish(InstallModel installModel)
        {
            DbExtension.InstallDb(installModel.ToDbConfig());
            var admin = new AdminEntity();
            admin.UserName = installModel.AdminUser;
            admin.Password = SM3.GetSM3(installModel.AdminPass);
            admin.Insert();
            var settings = new List<SettingsEntity>();
            settings.Add(new SettingsEntity()
            {
                Name = "title",
                Value = "JXCMS",
                Type = "Settings"
            });
            settings.Add(new SettingsEntity()
            {
                Name = "logo",
                Value = "/images/Logo.png",
                Type = "Settings"
            });
            settings.Add(new SettingsEntity()
            {
                Name = "keyword",
                Value = "JXCMS,急速开发CMS",
                Type = "Settings"
            });
            settings.Add(new SettingsEntity()
            {
                Name = "description",
                Value = "JXCMS是一款快速开发的CMS程序",
                Type = "Settings"
            });
            settings.Add(new SettingsEntity()
            {
                Name = "copyright",
                Value = "JXCMS 2019",
                Type = "Settings"
            });
            settings.Add(new SettingsEntity()
            {
                Name = "icp",
                Value = "",
                Type = "Settings"
            });
            BaseEntity.Orm.Insert(settings).ExecuteInserted();
            var classify = new ClassifyEntity()
            {
                Name = "电影",
                Alias = "dianying",
                ParentId = 0
            };
            classify.Save();
            List<ClassifyEntity> classifyEntities = new List<ClassifyEntity>();
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "动作片",
                Alias = "dongzuo",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "喜剧片",
                Alias = "xiju",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "爱情片",
                Alias = "aiqing",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "科幻片",
                Alias = "kehuan",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "恐怖片",
                Alias = "kongbu",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "剧情片",
                Alias = "juqing",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "战争片",
                Alias = "zhanzheng",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "纪录片",
                Alias = "jilupian",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "微电影",
                Alias = "weidianying",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "伦理片",
                Alias = "lunli",
                ParentId = classify.Id
            });
            BaseEntity.Orm.Insert(classifyEntities).ExecuteAffrows();
            classify = new ClassifyEntity()
            {
                Name = "电视剧",
                Alias = "tv",
                ParentId = 0
            };
            classify.Save();
            classifyEntities.Clear();
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "国产剧",
                Alias = "guochanju",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "香港剧",
                Alias = "xianggangju",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "韩国剧",
                Alias = "hanguoju",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "欧美剧",
                Alias = "oumeiju",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "台湾剧",
                Alias = "taiwanju",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "日本剧",
                Alias = "ribenju",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "海外剧",
                Alias = "haiwaiju",
                ParentId = classify.Id
            });
            BaseEntity.Orm.Insert(classifyEntities).ExecuteAffrows();
            classify = new ClassifyEntity()
            {
                Name = "综艺片",
                Alias = "zongyi",
                ParentId = 0
            };
            classify.Save();
            classifyEntities.Clear();
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "内地综艺",
                Alias = "neidizongyi",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "日韩综艺",
                Alias = "rihanzongyi",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "欧美综艺",
                Alias = "oumeizongyi",
                ParentId = classify.Id
            });
            BaseEntity.Orm.Insert(classifyEntities).ExecuteAffrows();
            classify = new ClassifyEntity()
            {
                Name = "动漫片",
                Alias = "dongman",
                ParentId = 0
            };
            classify.Save();
            classifyEntities.Clear();
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "国产动漫",
                Alias = "guochandongman",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "日韩动漫",
                Alias = "rihandongman",
                ParentId = classify.Id
            });
            classifyEntities.Add(new ClassifyEntity()
            {
                Name = "欧美动漫",
                Alias = "oumeidongman",
                ParentId = classify.Id
            });
            BaseEntity.Orm.Insert(classifyEntities).ExecuteAffrows();
            System.IO.File.Create("install.lock");
            return View();
        }
    }
}