using ASHermed.SinoInterface.Api.ContractModel;
using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.ContractModel.Framework.Login;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Api.IRepository.Quest;
using ASHermed.SinoInterface.Api.IService;
using ASHermed.SinoInterface.ContractModel;
using ASHermed.SinoInterface.Enum;
using ASHermed.SinoInterface.IFramework.SingleSignOn;
using ASHermed.SinoInterface.Tools;
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 IndexService : BaseService, IIndexService
    {
        IPubBannerRepository _pubBannerRepository;
        IOutAptInfoRepository _outAptInfoRepository;
        IDicHosRepository _dicHosRepository;
        IPubPatInfoRepository _pubPatInfoRepository;
        IPubQuestDataRepository _pubQuestDataRepository;
        IAuthorizationService _authorizationService;
        /// <summary>
        /// 首页所需接口
        /// </summary>
        public IndexService(IPubBannerRepository pubBannerRepository, IOutAptInfoRepository outAptInfoRepository, IDicHosRepository dicHosRepository, IPubPatInfoRepository pubPatInfoRepository, IPubQuestDataRepository pubQuestDataRepository, IAuthorizationService authorizationService)
        {
            _pubBannerRepository = pubBannerRepository;
            _outAptInfoRepository = outAptInfoRepository;
            _dicHosRepository = dicHosRepository;
            _pubPatInfoRepository = pubPatInfoRepository;
            _pubQuestDataRepository = pubQuestDataRepository;
            _authorizationService = authorizationService;
        }


        public List<Pub_Banner> GetBanner(int language, int showWhere)
        {
            return _pubBannerRepository.GetBanner(language, showWhere);
        }

        public async Task<BackgroundPicModel> GetBackgroundPic(int language)
        {
            BackgroundPicModel mdoel = new BackgroundPicModel();
            var ban = _pubBannerRepository.GetBanner(1, 4);
            if (ban?.Count > 0)
            {
                mdoel.BackgroundPic = ban.FirstOrDefault().ImgUrl;
            }
            var hos = _dicHosRepository.GetAllHosCity();
            mdoel.Cities = new List<City>();
            City city = null;
            foreach (var item in hos)
            {
                city = new City();
                city.CityName = item.CN_City;
                city.CityNameEn = item.EN_City;
                if (mdoel.Cities.Find(p => p.CityName == city.CityName) == null)
                {
                    mdoel.Cities.Add(city);
                }
            }
            return mdoel;
        }

        public async Task<List<AppletBtn>> GetFunction(int language)
        {
            var ban = await _pubBannerRepository.GetFunction(1, language);
            return ban;
        }

        public async Task<List<MissionModel>> GetMission(int language)
        {
            List<MissionModel> list = new List<MissionModel>();
            var user = GetBaseUserInfo();
            if (user == null)
            {
                return list;
            }
            List<Pub_Pat_Info> authPats = _authorizationService.GetUserAuthPats(user.ID, (int)AuthType.OFFLINE);
            List<AptPatData> apts = _outAptInfoRepository.GetOutAptPatByUser(authPats.Select(p=>p.Id).ToList());

            if (apts == null)
            {
                return list;
            }
            var missions = await _pubBannerRepository.GetFunction(2, language);
            if (missions == null || missions.Count == 0)
            {
                return list;
            }

            var questData = _pubQuestDataRepository.GetPatsFluQuest(apts.Select(p => p.PatId).ToList());
            MissionModel model = null;
            foreach (var apt in apts.OrderBy(p => p.AptTime))
            {
                foreach (var item in missions)
                {
                    model = new MissionModel();
                    //if (item.GoUrl == "1")
                    //{
                    //    var pat = pats.Find(p => p.Id == apt.PatId);
                    //    if (!string.IsNullOrEmpty(pat.EmergencyName) && pat.ContactWay > 0)
                    //    {
                    //        continue;
                    //    }
                    //}
                    if (item.GoUrl == "1")
                    {
                        continue;
                    }
                    else if (item.GoUrl == "2")
                    {
                        if (apt.AptTime.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"))
                        {
                            var quest = questData.Find(p => p.PatId == apt.PatId);
                            if (quest != null)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }


                    }
                    else if (item.GoUrl == "3")
                    {
                        if (apt.Service?.ToLower() != "con75")
                        {
                            continue;
                        }
                        var quest = _pubQuestDataRepository.GetPhysicalQuestData(apt.Id, 1);
                        if (quest != null && quest.Count > 0)
                        {
                            continue;
                        }
                        var pat = _pubPatInfoRepository.SelectById(apt.PatId);
                        if (pat != null)
                        {
                            model.Age = Convert.ToDateTime(pat.Birthday).GetAgeByBirth();
                            model.Gender = pat.Gender.Value;
                            model.PatBirthday = pat.Birthday;
                            model.PatPhone = pat.Phone_NO;
                            model.QuestId = 1;
                            model.Type = "update";
                            model.AptId = apt.Id;
                        }
                    }

                    model.GoUrl = "";
                    model.MissionName = item.BtnText;
                    model.Icon = item.BtnIcon;
                    model.MissionType = Convert.ToInt32(item.GoUrl);
                    model.PatName = apt.PatName;
                    model.PatId = apt.PatId;
                    model.PatMrn = apt.PatMrn;
                    if (list.Find(p => p.PatId == model.PatId && p.MissionType == model.MissionType && p.GoUrl == model.GoUrl && p.MissionType != 3) == null)
                    {
                        list.Add(model);
                    }

                }
            }
            var mission = missions.Find(p => p.GoUrl == "1");
            if (mission != null)
            {
                var pats = _pubPatInfoRepository.GetUserRelationship(user.ID);
                if (pats?.Count > 0)
                {
                    foreach (var item in pats)
                    {
                        if (!string.IsNullOrEmpty(item.EmergencyName) && item.PayType > 0)
                        {
                            continue;
                        }
                        model = new MissionModel();
                        model.GoUrl = "";
                        model.MissionName = mission.BtnText;
                        model.Icon = mission.BtnIcon;
                        model.MissionType = Convert.ToInt32(mission.GoUrl);
                        model.PatName = item.Pat_Name;
                        model.PatId = item.Id;
                        model.PatMrn = item.Pat_MRN;
                        list.Add(model);
                    }
                }
            }

            return list.Distinct().ToList();

        }


        public async Task<List<AptDoc>> GetHasConsultation(int language)
        {
            var user = GetBaseUserInfo();
            if (user == null)
            {
                return new List<AptDoc>();
            }
            List<Pub_Pat_Info> authPats = _authorizationService.GetUserAuthPats(user.ID, (int)AuthType.OFFLINE);
            List<AptDoc> apts = _outAptInfoRepository.GetHasConsultation(authPats.Select(p => p.Id).ToList(), language);
            //TODO加载近期问诊医生跳转地址
            foreach (var item in apts)
            {
                item.GoUrl = "";
                item.DeptNames = item.DeptNames?.Replace(",", " ");
            }
            return apts;
        }

        public async Task<List<HosInfo>> GetHosList(string cityName, int language)
        {

            var list = await _dicHosRepository.GetHosList(cityName, language);
            return list;
        }

    }
}
