using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.PTG;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ZhonTai.Admin.Core.Attributes;
using ZhonTai.Admin.Core.Consts;
using ZhonTai.Admin.Core.Dto;
using ZhonTai.Admin.Domain.Project;
using ZhonTai.Admin.Domain.User;
using ZhonTai.Admin.Repositories;
using ZhonTai.Admin.Repositories.Notice;
using ZhonTai.Admin.Repositories.Project;
using ZhonTai.Admin.Services.DiTuiAPI.Dto;
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;

namespace ZhonTai.Admin.Services.DiTuiAPI
{
    /// <summary>
    /// 首页接口
    /// </summary>
    /// <returns></returns>
    [DynamicApi(Area = AdminConsts.DiTuiName)]
    public class IndexService : BaseService, IIndexService, IDynamicApi
    {
        private ProjectRepository _projectRepository;
        private NoticeRepository _noticeRepository;
        private OrgRepository _orgRepository;

        public IndexService(ProjectRepository projectRepository,NoticeRepository noticeRepository,OrgRepository orgRepository)
        {
            _projectRepository = projectRepository;
            _noticeRepository = noticeRepository;
            _orgRepository = orgRepository;
        }

        /// <summary>
        /// 首页信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [NoOprationLog]
        public async Task<IndexOutput> IndexAsync()
        {
            var uId = User?.Id;
            var tenantId = User.TenantId;
            IndexOutput indexOutPut = await _orgRepository.Select.DisableGlobalFilter()
                .Where(a => a.TenantId == User.TenantId)
                .FirstAsync(a => new IndexOutput(a.Id, a.Name));



            //IndexOutput indexOutPut = new IndexOutput();
            //indexOutPut.Name = "99地推";
            indexOutPut.Tips = "推广要求:杜绝任何虚假、作弊、违法行为!";

            List<Imgs> imgsList = new List<Imgs>();
            imgsList.Add(new Imgs(
                "https://test-dt.zhongjie51.com/assetsImg/lunbo1.png",
                "1",
                "11",
                "2023-01-11 11:11:11",
                "2023-11-11 11:11:11",
                "99"
                ));
            imgsList.Add(new Imgs(
                "https://test-dt.zhongjie51.com/assetsImg/lunbo2.png",
                "2",
                "222",
                "2023-02-02 12:12:12",
                "2023-12-22 14:59:49",
                "99"
                ));
            imgsList.Add(new Imgs(
                "https://test-dt.zhongjie51.com/assetsImg/lunbo3.png",
                "3",
                "333",
                "2023-03-03 12:12:12",
                "2023-12-30 14:59:49",
                "99"
                ));

            indexOutPut.Imgs = imgsList;


            //List<Notices> notices = new List<Notices>();
            // 公告
            //notices.Add(new Notices("最新","平台悬赏任务、游戏试玩任务上线通知…","11"));
            //notices.Add(new Notices("重要", "快手极速版直推版价格政策调整通知", "22"));
            //notices.Add(new Notices("最新", "12月平台项目重要通知请注意查看!12月平台项目重要通知请注意查看!", "33"));
            var notices = _noticeRepository.Select.DisableGlobalFilter(FilterNames.Tenant)
                .OrderByDescending(m=>m.Rank)
                .ToList(m => new Notices()
                {
                    Id = m.Id.ToString(),
                    Title = m.Title,
                    Tags = m.Tags
                });

            indexOutPut.Notices = notices;

            List<Settlement> settlements = new List<Settlement>();
            settlements.Add(new Settlement("抖音电商1分购-高价版", "未结算"));
            settlements.Add(new Settlement("京喜特价-发货版", "已结算至03月20日"));
            settlements.Add(new Settlement("抖音电商1分购-稳定版", "已结算至03月20日"));
            settlements.Add(new Settlement("兴业数字人民币", "已结算至03月20日"));
            settlements.Add(new Settlement("抖音极速地推版", "已结算至03月20日"));
            settlements.Add(new Settlement("易行通ETC(自助发货)", "已结算至03月20日"));
            settlements.Add(new Settlement("抖美团外卖-关注公众号", "已结算至03月20日"));
            settlements.Add(new Settlement("抖音电商1分购-高价版", "已结算至03月20日"));
            settlements.Add(new Settlement("抖音电商1分购-高价版", "已结算至03月20日"));
            ;
            indexOutPut.settlement = settlements;

            return indexOutPut;
        }

        /// <summary>
        /// 首页项目
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [NoOprationLog]
        public async Task<ProjectOutput> ProjectAsync(string Status)
        {
            //Expression<Func<ProjectEntity, bool>> where = (a => a.UserName == input.UserName);
            Expression<Func<ProjectEntity, bool>> where = null;
            //where = where.Or(input.Mobile.NotNull(), a => a.Mobile == input.Mobile)
            //    .Or(input.Email.NotNull(), a => a.Email == input.Email);

            if(Status == "1")
            {
                where = (a => a.Status == 2 || a.Status == 5);
            }
            else if(Status == "0")

            {
                where = (a => a.Status == 4);
            }
            else
            {
                throw ResultOutput.Exception($"参数不正确");
            }
            //return 
            //throw ResultOutput.Exception($"错误提示");
            //ProjectOutput projectOutput = new ProjectOutput();

            //List<ProjectOutPut> projectOutPuts = new List<ProjectOutPut>();

            var result = _projectRepository.Where(where)
              .DisableGlobalFilter(FilterNames.Tenant)
              .Where(a => a.TenantId == User.TenantId || a.TenantId == 161223412138053)
              // ProjectOutPut
              .ToListAsync(a => new ProjectOutPut { Id = a.Id, Name = a.Name, Logo = a.Logo, Tips = a.Tips, HotWords = a.HotWords, StatusWords = a.Status.ToString(), StatusValue = a.Status.ToString() });

            ProjectOutput projectOutput = new ProjectOutput();

            List<ProjectOutPut> projectOutPuts = new List<ProjectOutPut>();
            

            foreach (ProjectOutPut item in result.Result)
            {
                switch (item.StatusWords)
                {
                    case "5":
                        item.StatusWords = "名额已满";
                        break;
                    case "4":
                        item.StatusWords = "项目暂停";
                        break;
                    case "2":
                        item.StatusWords = "立即推广";
                        break;
                }

                if (item.StatusValue == "4")
                {
                    item.StatusValue = "1";
                    projectOutPuts.Add(item);
                }
                else
                {
                    if (item.StatusValue == "5")
                    {
                        item.StatusValue = "0";
                    }
                    else
                    {
                        item.StatusValue = "1";
                    }

                    projectOutPuts.Add(item);
                }
            }

            projectOutput.Count = projectOutPuts.Count.ToString();
            projectOutput.Projects = projectOutPuts;



            //if (flag)
            //{
            //    projectOutPuts.Add(
            //    new ProjectOutPut(
            //        414497860591685,
            //        "快手极速版",
            //        "https://test-dt.zhongjie51.com/assetsImg/ks_logo.png",
            //        "快手极速版APP的新用户，全国可做",
            //        "热门,高佣,简单,T+1",
            //        "立即推广",
            //        "2"
            //        )
            //    );

            //    projectOutPuts.Add(
            //        new ProjectOutPut(
            //            414497860591686,
            //            "京东白条",
            //            "https://test-dt.zhongjie51.com/assetsImg/jd_logo.png",
            //            "全国可做。用户激活白条后并下单支付10元..",
            //            "最新,简单,热门,推荐",
            //            "名额已满",
            //            "5"
            //            )
            //        );
            //    projectOutPuts.Add(
            //        new ProjectOutPut(
            //            414497860591687,
            //            "点淘拉新",
            //            "https://test-dt.zhongjie51.com/assetsImg/dt_logo.png",
            //            "全国可做。点淘手机端新用户，官方直推",
            //            "热门,赚钱,官方,简单",
            //            "立即推广",
            //            "2"
            //            )
            //        );
            //}
            //else
            //{
            //    projectOutPuts.Add(
            //    new ProjectOutPut(
            //        414497860591688,
            //        "点淘拉新（暂停）",
            //        "https://test-dt.zhongjie51.com/assetsImg/dt_logo.png",
            //        "全国可做。点淘手机端新用户，官方直推",
            //        "热门,赚钱,官方,简单",
            //        "项目暂停",
            //        "4"
            //        )
            //    );
            //    projectOutPuts.Add(
            //        new ProjectOutPut(
            //            414497860591689,
            //            "京东白条",
            //            "https://test-dt.zhongjie51.com/assetsImg/jd_logo.png",
            //            "全国可做。用户激活白条后并下单支付10元..",
            //            "最新,简单,热门,推荐",
            //            "项目暂停",
            //            "4"
            //            )
            //        );
            //}
            
            
            //projectOutput.Count = projectOutPuts.Count.ToString();
            //projectOutput.Projects = projectOutPuts;

            return projectOutput;
        }

        private static bool IsInt(string inString)
        {
            Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
            return regex.IsMatch(inString.Trim());
        }
    }
}
