using Furion.DependencyInjection;
using Furion.ViewEngine;
using Microsoft.AspNetCore.Http;
using Nirvana.Common;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YBDevice.Entity;

namespace YBDevice.Application.ViewInfo
{
    public class ViewEngineService : IViewEngineService, ITransient
    {
        private readonly ISqlSugarRepository<YB_nMenu> repository;
        private readonly SqlSugarClient dbClient;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IViewEngine _viewEngine;
        private readonly OperatorModel currentUser;

        public ViewEngineService(ISqlSugarRepository<YB_nMenu> sqlSugarRepository, IHttpContextAccessor httpContextAccessor, IViewEngine viewEngine)
        {
            repository = sqlSugarRepository;
            dbClient = repository.Context;
            _httpContextAccessor = httpContextAccessor;
            _viewEngine = viewEngine;
            currentUser = BaseInfoService.GetUserInfo();
        }

        /// <summary>
        /// 获取指定页面的按钮列表
        /// </summary>
        /// <returns></returns>
        public async Task<ViewS2SDto> GetBtnListAsync()
        {
            //根据请求的控制器和方法确定按钮列表
            var request = _httpContextAccessor.HttpContext.Request;
            var path = request.Path.Value;
            var arr = path.Split('/');
            if (arr.Length != 3)
            {
                return new ViewS2SDto();
            }
            string controlname = arr[1].ToLower();
            string actionname = arr[2].ToLower();
            var list = new List<YB_nMenuAction>();
            if (currentUser.IsSuper)
            {
                list = await dbClient.Queryable<YB_nMenuAction>().Where(x =>
                            x.ControlName == controlname
                            && x.ActionName == actionname)
                .OrderBy(x => x.SortCode, OrderByType.Asc)
                .ToListAsync();
            }
            else
            {
                list = await dbClient.Queryable<YB_nMenuAction>()
                  .Where(x => SqlFunc.Subqueryable<YB_nRoleAction>()
                              .Where(e => e.RoleId == currentUser.RoleId && e.ButtonId == x.Id).Any()
                              && x.ControlName == controlname
                              && x.ActionName == actionname)
                  .OrderBy(x => x.SortCode, OrderByType.Asc)
                  .ToListAsync();
            }
            var toplist = list.Where(x => x.Type == MenuButtonPosition.Top).ToList();
            var tabletoplist = list.Where(x => x.Type == MenuButtonPosition.TableTop).ToList();
            var tablelist = list.Where(x => x.Type == MenuButtonPosition.Table).ToList();
            var data = new ViewS2SDto();
            if (toplist != null && toplist.Count > 0)
            {
                data.Top = await _viewEngine.RunCompileFromCachedAsync(@"
                @foreach(var item in Model)
                {
                   <button class='btn btn-primary btn-lg  js-@item.ButtonId' type='button'>@item.ButtonName</button>
                }
                ", toplist);
            }
            if (tabletoplist != null && tabletoplist.Count > 0)
            {
                if (controlname == "code" && actionname == "template")
                {
                    data.TableTop = await _viewEngine.RunCompileFromCachedAsync(@"
                        @foreach(var item in Model)
                        {
                           @if(item.ButtonId ==""tpl"")
                            {
                              <a href='#' class='js-@item.ButtonId' title='@item.ButtonName' data-id='{{d.id}}'>@item.ButtonName</a>
                            }
                            else{
                                <button class='btn btn-primary btn-lg' lay-event='js-@item.ButtonId'>@item.ButtonName</button>
                            }
                        }", tabletoplist);
                }
                else
                {
                    data.TableTop = await _viewEngine.RunCompileFromCachedAsync(@"
                @foreach(var item in Model)
                {
                   <button class='btn btn-primary btn-lg' lay-event='js-@item.ButtonId'>@item.ButtonName</button>
                }
                ", tabletoplist);
                }
            }
            if (tablelist != null && tablelist.Count > 0)
            {
                if (controlname == "device" && actionname == "index")
                {
                    data.Table = await _viewEngine.RunCompileFromCachedAsync(@"
                    @foreach(var item in Model)
                    {
                       @if(item.ButtonId ==""Set"")
                        {
                            @:{{# if(d.status == 0){ }}
                             <a href='#' class='js-@item.ButtonId' data-status='1' title='启用' data-id='{{d.id}}'>启用</a>
                            @:{{# }else if(d.status==1){ }}
                             <a href='#' class='js-@item.ButtonId' data-status='0' title='停用' data-id='{{d.id}}'>停用</a>
                           @:{{# } }}
                        }
                        else{
                            <a href='#' class='js-@item.ButtonId' title='@item.ButtonName' data-id='{{d.id}}'>@item.ButtonName</a>
                        }
                    }", tablelist);
                }
                else if (controlname == "miniprogram" && actionname == "index")
                {
                    data.Table = await _viewEngine.RunCompileFromCachedAsync(@"
                    @foreach(var item in Model)
                    {
                       @if(item.ButtonId ==""detail"" || item.ButtonId ==""tycode"" || item.ButtonId ==""Update"" || item.ButtonId ==""Tpl"" || item.ButtonId ==""CService"")
                        {
                            @:{{# if(d.service_type_info !=""小程序""){ }}
                              <a href='#' class='js-@item.ButtonId' title='@item.ButtonName' data-id='{{d.id}}'>@item.ButtonName</a>
                            @:{{# }else{ }}

                           @:{{# } }}
                        }
                        else{
                            <a href='#' class='js-@item.ButtonId' title='@item.ButtonName' data-id='{{d.id}}'>@item.ButtonName</a>
                        }
                    }", tablelist);
                }
                else if (controlname == "order" && actionname == "index")
                {
                    data.Table = await _viewEngine.RunCompileFromCachedAsync(@"
                    @foreach(var item in Model)
                    {
                       @if(item.ButtonId ==""Set"")
                        {
                            @:{{# if(d.status == 1){ }}
                             <a href='#' class='js-@item.ButtonId' data-status='2' title='暂停' data-id='{{d.id}}'>暂停</a>
                            @:{{# }else if(d.status==2){ }}
                             <a href='#' class='js-@item.ButtonId' data-status='1' title='开启' data-id='{{d.id}}'>开启</a>
                             <a href='#' class='js-@item.ButtonId' data-status='4' title='取消' data-id='{{d.id}}'>取消</a>
                           @:{{# } }}
                        }
                        else if(item.ButtonId == ""edit""){
                             @:{{# if(d.status == 1){ }}
                                @:{{# }else if(d.status==2){ }}
                                 <a href='#' class='js-@item.ButtonId' title='编辑' data-id='{{d.id}}'>编辑</a>
                               @:{{# } }}
                        }
                        else{
                            <a href='#' class='js-@item.ButtonId' title='@item.ButtonName' data-id='{{d.id}}'>@item.ButtonName</a>
                        }
                    }", tablelist);
                }
                else if ((controlname == "banner" || controlname == "info") && actionname == "index")
                {
                    data.Table = await _viewEngine.RunCompileFromCachedAsync(@"
                    @foreach(var item in Model)
                    {
                       @if(item.ButtonId ==""Set"")
                        {
                            @:{{# if(d.status == 0){ }}
                             <a href='#' class='js-@item.ButtonId' data-status='1' title='发布' data-id='{{d.id}}'>发布</a>
                            @:{{# }else{ }}
                             <a href='#' class='js-@item.ButtonId' data-status='0' title='下架' data-id='{{d.id}}'>下架</a>
                           @:{{# } }}
                        }
                        else{
                            <a href='#' class='js-@item.ButtonId' title='@item.ButtonName' data-id='{{d.id}}'>@item.ButtonName</a>
                        }
                    }", tablelist);
                }
                else if (controlname == "info" && actionname == "type")
                {
                    data.Table = await _viewEngine.RunCompileFromCachedAsync(@"
                    @foreach(var item in Model)
                    {
                       @if(item.ButtonId ==""Set"")
                        {
                            @:{{# if(d.status == 1){ }}
                             <a href='#' class='js-@item.ButtonId' data-status='0' title='禁用' data-id='{{d.id}}'>禁用</a>
                            @:{{# }else{ }}
                             <a href='#' class='js-@item.ButtonId' data-status='1' title='启用' data-id='{{d.id}}'>启用</a>
                           @:{{# } }}
                        }
                        else{
                            <a href='#' class='js-@item.ButtonId' title='@item.ButtonName' data-id='{{d.id}}'>@item.ButtonName</a>
                        }
                    }", tablelist);
                }
                else if (controlname == "result" && actionname == "index")
                {
                    data.Table = await _viewEngine.RunCompileFromCachedAsync(@"
                        @foreach(var item in Model)
                        {
                           @if(item.ButtonId ==""print"")
                            {
                                @:{{# if(d.devtype == 2 || d.devtype ==9 || d.devtype ==11 || d.devtype == 17 || d.devtype == 23 || d.devtype == 36 || d.devtype == 37){ }}
                                 <a href='#' class='js-@item.ButtonId' title='@item.ButtonName' data-id='{{d.id}}'>@item.ButtonName</a>
                                @:{{# }else{ }}

                               @:{{# } }}
                            }
                            else{
                                <a href='#' class='js-@item.ButtonId' title='@item.ButtonName' data-id='{{d.userid}}'>@item.ButtonName</a>
                            }
                        }", tablelist);
                }
                else if (controlname == "outproduct" && actionname == "index")
                {
                    data.Table = await _viewEngine.RunCompileFromCachedAsync(@"
                        @foreach(var item in Model)
                        {
                           @if(item.ButtonId ==""Set"")
                            {
                                   @:{{# if(d.status == 0){ }}
                                        <a href='#' class='js-@item.ButtonId' data-status='1' title='设备已打包' data-id='{{d.id}}'>打包</a>
                                   @:{{# }else if(d.status == 1){ }}
                                         <a href='#' class='js-@item.ButtonId' data-status='3' title='已发货' data-id='{{d.id}}'>已发货</a>
                                   @:{{# }else if(d.status == 3){ }}
                                         <a href='#' class='js-@item.ButtonId' data-status='4' title='已收货' data-id='{{d.id}}'>已收货</a>
                                   @:{{# } }}
                            }else if(item.ButtonId == ""cancel""){
                                   @:{{# if(d.status != 4 && d.status != 5){ }}
                                       <a href='#' class='js-@item.ButtonId' data-status='5' title='取消' data-id='{{d.id}}'>取消</a>
                                   @:{{# } }}
                            }
                            else{
                                <a href='#' class='js-@item.ButtonId' title='@item.ButtonName' data-id='{{d.id}}'>@item.ButtonName</a>
                            }
                        }", tablelist);
                }
                else
                {
                    data.Table = await _viewEngine.RunCompileFromCachedAsync(@"
                        @foreach(var item in Model)
                        {
                           @if(item.ButtonId ==""Set"")
                            {
                                @:{{# if(d.status == 0){ }}
                                 <a href='#' class='js-@item.ButtonId' data-status='1' title='启用' data-id='{{d.id}}'>启用</a>
                                @:{{# }else if(d.status==1){ }}
                                 <a href='#' class='js-@item.ButtonId' data-status='0' title='停用' data-id='{{d.id}}'>停用</a>
                               @:{{# } }}
                            }
                            else{
                                <a href='#' class='js-@item.ButtonId' title='@item.ButtonName' data-id='{{d.id}}'>@item.ButtonName</a>
                            }
                        }", tablelist);
                }
            }
            return data;
        }
    }
}