using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
using Api.SignToken;
using MadRunFabric.Common;
using System.Threading;
using System.Security.Claims;
using ConfigurationApi.IRepositories;
using MongoDB.Driver;
using MadRunFabric.Model;
using ConfigurationApi.Models.Input;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ConfigurationApi.Controllers
{
    /// <summary>
    /// 项目配置APP模块控制器
    /// </summary>
    [Authorize]
    [Route("api/[controller]")]
    public class AppMenuFunctionController : BaseController
    {

        private readonly ILogger<AppMenuFunctionController> _logger;

        private readonly ISys_AppModule_FunctionRepository _sys_appmodule_functionRepository;
        private readonly ISys_AppModule_InfoRepository _sys_appmodule_infoRepository;
        private readonly ISys_SystemConfigRepository _sys_systemconfigRepository;

        public AppMenuFunctionController(ISys_SystemConfigRepository sys_systemconfigRepository,ISys_AppModule_FunctionRepository sys_appmodule_functionRepository, ISys_AppModule_InfoRepository sys_appmodule_infoRepository, ILogger<AppMenuFunctionController> logger)
        {
            _sys_appmodule_functionRepository = sys_appmodule_functionRepository;
            _sys_appmodule_infoRepository = sys_appmodule_infoRepository;
  
            _logger = logger;
        }

        /// <summary>
        /// 保存项目关联
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> Add(AppModuleFunctionInput input)
        {
            var entity_ModuleInfo = new Sys_AppModule_Function();

            entity_ModuleInfo.module_id= input.module_id;
            entity_ModuleInfo.project_id = input.project_id;
            entity_ModuleInfo.sort = input.sort;
            entity_ModuleInfo.state_flag = 1;


            if (await _sys_appmodule_functionRepository.Add(entity_ModuleInfo))
            {
                return Success("添加成功");
            }
            else
            {
                return Error("菜单添加失败,请重试！");
            }

        }

        /// <summary>
        /// 删除项目关联
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<IActionResult> Remove(string mid)
        {
            if (await _sys_appmodule_functionRepository.Remove(mid))
            {
                return Success("删除成功");
            }
            else
            {
                return Error("删除失败,请重试！");
            }

        }

        /// <summary>
        /// 依据项目ID获取此项目的APP 模块配置
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet("getappmodule1")]
        public async Task<IActionResult> GetAppModuleAsync1(string project_id,int type=1,bool onlyopen=false)
        {
            var moduleInfoList = (await _sys_appmodule_infoRepository.Get(x => x.state_flag == 1&&x.ismenu==type)).OrderBy(x => x.sort).ThenByDescending(x => x.lastmodify_time).ToList();
            var moduleFunctionlist = (await _sys_appmodule_functionRepository.Get(x => x.project_id == project_id)).OrderBy(x => x.sort);

            var infoList = new List<Models.Dto.AppModuleInfoDto>();

            foreach (var item in moduleInfoList.ToArray())
            {
               var itemurl = "http://117.160.143.199:28270";
                ////Sys_SystemConfig dModel = await _sys_systemconfigRepository.GetSingle(p=>p.paramcode== "FilePath");
                //if (dModel != null)
                //{
                //    itemurl = dModel.paramvalue;
                //}
                //else
                //{
                //    itemurl = "http://117.160.143.199:28270";
                //}
                if (item.img_url != null)
                {
                    foreach (var item1 in item.img_url)
                    {
                        if(item1.fileurl.Contains("http://192.168.5.46"))
                         {
                            item1.fileurl = item1.fileurl.Replace("http://117.160.143.199:28270", itemurl);
                        }
                        
                    }
                }
                var _function = moduleFunctionlist.Where(t => (t.module_id == item.id));
                Models.Dto.AppModuleInfoDto info = new Models.Dto.AppModuleInfoDto();
                info.function_id = _function.Count() > 0 ? _function.First().id : "";
                info.project_id= _function.Count() > 0 ? _function.First().project_id : "";
                info.type = item.ismenu;
                info.module_id = item.id;
                info.module_name = item.module_name;
                info.module_code = item.module_code;
                info.module_imgurl = item.img_url;
                info.module_remark = item.remark;
                info.module_sort = item.sort;
                info.project_sort =_function.Count() > 0 ? _function.First().sort : 1;
                info.flag = _function.Count() > 0 ? 1 : 0;
                infoList.Add(info);
            }
            if (onlyopen)
            {
                return Success("获取项目模块成功！", infoList.Where(x => x.flag==1).OrderBy(x => x.project_sort).OrderBy(x => x.module_sort));
            }

            return Success("获取项目模块成功！", infoList.OrderByDescending(x=>x.flag).ThenBy(x => x.project_sort).ThenBy(x=>x.module_sort));
        }


        [HttpGet("getappmodule")]
        public async Task<IActionResult> GetAppModuleAsync(string project_id, int type = 1, bool onlyopen = false)
        {
            var moduleInfoList = (await _sys_appmodule_infoRepository.Get(x => x.state_flag == 1 && x.ismenu == type)).OrderBy(x => x.sort).ThenByDescending(x => x.lastmodify_time).ToList();
            var moduleFunctionlist = (await _sys_appmodule_functionRepository.Get(x => x.project_id == project_id)).OrderBy(x => x.sort);

            var infoList = new List<Models.Dto.AppModuleInfoDto>();

            foreach (var item in moduleInfoList.ToArray())
            {
                var itemurl = "http://117.160.241.16";
                ////Sys_SystemConfig dModel = await _sys_systemconfigRepository.GetSingle(p=>p.paramcode== "FilePath");
                //if (dModel != null)
                //{
                //    itemurl = dModel.paramvalue;
                //}
                //else
                //{
                //    itemurl = "http://117.160.143.199:28270";
                //}
                if (item.img_url != null)
                {
                    foreach (var item1 in item.img_url)
                    {
                        if (item1.fileurl.Contains("http://192.168.5.46"))
                        {
                            item1.fileurl = item1.fileurl.Replace("http://192.168.5.46", itemurl);
                        }
                        if (item1.fileurl.Contains("117.160.143.199"))
                        {
                            item1.fileurl = item1.fileurl.Replace("http://117.160.143.199", itemurl);
                        }
                        if (item1.fileurl.Contains("http://172.16.3.72:28270"))
                        {
                            item1.fileurl = item1.fileurl.Replace("http://172.16.3.72:28270", "http://39.164.132.51:28000/fileserverapi");
                        }
                        if (item1.filesmallurl.Contains("http://192.168.5.46"))
                        {
                            item1.filesmallurl = item1.filesmallurl.Replace("http://192.168.5.46", itemurl);
                        }
                        if (item1.filesmallurl.Contains("117.160.143.199"))
                        {
                            item1.filesmallurl = item1.filesmallurl.Replace("http://117.160.143.199", itemurl);
                        }
                        if (item1.filesmallurl.Contains("http://172.16.3.72:28270"))
                        {
                            item1.filesmallurl = item1.filesmallurl.Replace("http://172.16.3.72:28270", "http://39.164.132.51:28000/fileserverapi");
                        }

                    }
                }
                var _function = moduleFunctionlist.Where(t => (t.module_id == item.id));
                Models.Dto.AppModuleInfoDto info = new Models.Dto.AppModuleInfoDto();
                info.function_id = _function.Count() > 0 ? _function.First().id : "";
                info.project_id = _function.Count() > 0 ? _function.First().project_id : "";
                info.type = item.ismenu;
                info.module_id = item.id;
                info.module_name = item.module_name;
                info.module_code = item.module_code;
                info.module_imgurl = item.img_url;
                info.module_remark = item.remark;
                info.module_sort = item.sort;
                info.project_sort = _function.Count() > 0 ? _function.First().sort : 1;
                info.flag = _function.Count() > 0 ? 1 : 0;
                infoList.Add(info);
            }
            if (onlyopen)
            {
                return Success("获取项目模块成功！", infoList.Where(x => x.flag == 1).OrderBy(x => x.project_sort).OrderBy(x => x.module_sort));
            }

            return Success("获取项目模块成功！", infoList.OrderByDescending(x => x.flag).ThenBy(x => x.project_sort).ThenBy(x => x.module_sort));
        }



    }
}
