using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.ContractModel.Framework.Login;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Api.IService;
using ASHermed.SinoInterface.ContractModel;
using ASHermed.SinoInterface.IFramework.SingleSignOn;
using ASHermed.SinoInterface.Tools;
using Newtonsoft.Json;
using PoJun.Util.Maps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASHermed.SinoInterface.Api.Service
{
    /// <summary>
	/// 诊室
	/// </summary>
	public class DeptService : BaseService, IDeptService
    {
        IDicDeptRepository _dicDeptRepository;
        IDoctorService _doctorService;
        RedisHelperClient _redisHelper;
        IDicDoctorRepository _dicDoctorRepository;
        /// <summary>
        /// 诊室
        /// </summary>
        public DeptService(IDicDeptRepository dicDeptRepository, IDoctorService doctorService, RedisHelperClient redisHelper, IDicDoctorRepository dicDoctorRepository)
        {
            _dicDeptRepository = dicDeptRepository;
            _doctorService = doctorService;
            _redisHelper = redisHelper;
            _dicDoctorRepository = dicDoctorRepository;
        }


        public List<DeptByLanguage> SelectByLanguage(string filter, int language, int hosId = 0)
        {
            List<DeptByLanguage> list = _dicDeptRepository.SelectByLanguage(language);
            if (hosId > 0)
            {
                var relist = _dicDeptRepository.GetHosAllDepts(hosId);
                if (relist?.Count > 0)
                {
                    list = list.FindAll(p => relist.Select(q => q.Apt_Dept_Id).ToList().Contains(p.Id));
                }
            }
            if (!string.IsNullOrEmpty(filter))
            {
                //判断是否为拼音或者英文
                if (!filter.IsChinese())
                {
                    //转换为数组
                    string[] filterPinyinArr = filter.Replace("  ", " ").ToLower().Split(' ');
                    List<DeptByLanguage> pinyinFindList = new List<DeptByLanguage>();
                    //循环数据列表
                    foreach (var dept in list)
                    {
                        //获取科室名称拼音
                        string[] deptPinyinArr = dept.NamePinyin?.Split(',');
                        //匹配拼音
                        if (Common.ContainArray(deptPinyinArr, filterPinyinArr))
                        {
                            pinyinFindList.Add(dept);
                        }
                        else
                        {
                            deptPinyinArr = dept.Name.ToLower().Replace("  ", " ").Split(' ');
                            if (Common.ContainArray(deptPinyinArr, filterPinyinArr))
                            {
                                pinyinFindList.Add(dept);
                            }
                            else
                            {
                                deptPinyinArr = dept.YName.Replace("  ", " ").ToLower().Split(' ');
                                if (Common.ContainArray(deptPinyinArr, filterPinyinArr))
                                {
                                    pinyinFindList.Add(dept);
                                }
                                else
                                {
                                    if (dept.NameCn.Contains(filter))
                                    {
                                        pinyinFindList.Add(dept);
                                    }
                                }
                            }
                        }

                    }

                    list = pinyinFindList;
                }
                else
                {
                    //list = list.stream().filter(
                    //        o-> (o.getName() != null && o.getName().toLowerCase().contains(filter.toLowerCase())))
                    //        .collect(Collectors.toList());
                    list = list.FindAll(p => (!string.IsNullOrEmpty(p.NameCn) && p.NameCn.ToLower().Contains(filter.ToLower())) || (!string.IsNullOrEmpty(p.NameEn) && p.NameEn.ToLower().Contains(filter.ToLower())));
                }
            }
            return list;
        }

        public List<DeptByLanguage> GetAllOnlineDepts(int language)
        {
            List<DeptByLanguage> list = _dicDeptRepository.SelectByLanguage(language);
            var onlineDepts = _dicDoctorRepository.GetAllOnlineDeptInfo();
            var deptIdList = onlineDepts.Select(p => p.DeptId).Distinct().ToList();
            list = list.FindAll(p => deptIdList.Contains(p.Id));
            return list;
        }

        public List<DeptByLanguage> GetInsurDept(int language, int type)
        {
            List<DeptByLanguage> list = _dicDeptRepository.GetInsurDept(type, language);

            if (language == 1)
            {
                list.Insert(0, new DeptByLanguage() { Id = 0, Name = "全部科室" });
            }
            else
            {
                list.Insert(0, new DeptByLanguage() { Id = 0, Name = "All" });
            }
            return list;
        }

        public List<DeptByLanguage> SelectOnlineByLanguage(string filter, int language, int type)
        {
            List<DeptByLanguage> list = new List<DeptByLanguage>();
            if (type == 3)
            {//今天出诊医生
                var onlineDepts = _dicDoctorRepository.GetAllOnlineDeptDocInfo();
                var docIds = _redisHelper.Get("DocSlot_" + DateTime.Now.ToString("MMdd"));
                var docIdList = JsonConvert.DeserializeObject<List<int>>(docIds);
                DeptByLanguage dept = null;
                foreach (var item in docIdList)
                {
                    var odept = onlineDepts.Find(p => p.DocId == item);
                    if (odept == null || list.Find(p => p.Id == odept.DeptId) != null)
                    {
                        continue;
                    }
                    if (odept != null)
                    {
                        dept = new DeptByLanguage();
                        dept.Id = odept.DeptId;
                        dept.Name = language == 1 ? odept.DeptNameCn : odept.DeptNameEn;
                        list.Add(dept);
                    }
                }
            }
            else
            {
                list = _dicDeptRepository.GetOnlineDepts(type, language);
                if (!string.IsNullOrEmpty(filter))
                {
                    //判断是否为拼音或者英文
                    if (!filter.IsChinese())
                    {
                        //转换为数组
                        string[] filterPinyinArr = filter.Replace("  ", " ").ToLower().Split(' ');
                        List<DeptByLanguage> pinyinFindList = new List<DeptByLanguage>();
                        //循环数据列表
                        foreach (var dept in list)
                        {
                            //获取科室名称拼音
                            string[] deptPinyinArr = dept.NamePinyin?.Split(',');
                            //匹配拼音
                            if (Common.ContainArray(deptPinyinArr, filterPinyinArr))
                            {
                                pinyinFindList.Add(dept);
                            }
                            else
                            {
                                deptPinyinArr = dept.Name.ToLower().Replace("  ", " ").Split(' ');
                                if (Common.ContainArray(deptPinyinArr, filterPinyinArr))
                                {
                                    pinyinFindList.Add(dept);
                                }
                                else
                                {
                                    deptPinyinArr = dept.YName.Replace("  ", " ").ToLower().Split(' ');
                                    if (Common.ContainArray(deptPinyinArr, filterPinyinArr))
                                        pinyinFindList.Add(dept);
                                }
                            }
                        }

                        list = pinyinFindList;
                    }
                    else
                    {
                        //list = list.stream().filter(
                        //        o-> (o.getName() != null && o.getName().toLowerCase().contains(filter.toLowerCase())))
                        //        .collect(Collectors.toList());
                        list = list.FindAll(p => !string.IsNullOrEmpty(p.Name) && p.Name.ToLower().Contains(filter.ToLower()));
                    }
                }

            }
            if (language == 1)
            {
                list.Insert(0, new DeptByLanguage() { Id = 0, Name = "全部科室" });
            }
            else
            {
                list.Insert(0, new DeptByLanguage() { Id = 0, Name = "All" });
            }
            return list;
        }




        public List<DeptModel> SelectIndexDeptByLanguage(int language)
        {
            return _dicDeptRepository.SelectIndexDeptByLanguage(language);
        }



        public DeptModel GetDeptById(int id, int language, string userAgent)
        {
            var dept = _dicDeptRepository.GetDeptById(id, language);
            if (dept != null)
            {
                if (string.IsNullOrEmpty(userAgent))
                {
                    dept.Doclist = _doctorService.FindTeamDocList(false, id, language);
                }
                else
                {
                    if (userAgent == "cigna")
                    {
                        dept.Doclist = _doctorService.FindInsurTeamDocList(3, id, language);
                    }
                }

            }
            return dept;
        }

        public List<int> GetDeptIdIds(int doctorId)
        {
            return _dicDeptRepository.GetDeptIdIds(doctorId);
        }


        public List<DeptModel> GetCignaDepts(int language)
        {
            return _dicDeptRepository.GetCignaDepts(language, "");
        }

        public List<DeptModel> GetCignaHomeDepts(int language)
        {
            return _dicDeptRepository.GetCignaDepts(language, "")?.Take(4).ToList();
        }

        public CignaSearchResult CignaSearch(int language, string keyWords)
        {
            CignaSearchResult res = new CignaSearchResult();
            res.DeptList = _dicDeptRepository.GetCignaDepts(language, keyWords);
            res.DocList = _dicDoctorRepository.GetInsurDocList(3, 0, language, keyWords);
            return res;
        }



    }
}
