using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pluto.Api;
using McKesson.PPS.Fusion.Authentication.Business;
using McKesson.PPS.Fusion.Terminology.Business;
using McKesson.PPS.Fusion.Business.Shared.Terminology;

namespace PlutoServer.PracticeChoice.Core {
    public class Terminology {

        public IList<TerminologyInfo1> SearchTerminology(string authToken, string searchString, TerminologySearchTypeEnum searchType, TerminologyDomainEnum domain) {
            IList<TerminologyInfo1> termList = null;
            MagicIdentity identity = MagicIdentity.FromCookieString(authToken);
            if(identity.IsAuthenticated) {
                MagicPrincipal principal = new MagicPrincipal(identity);

                //Logger.Write(string.Format("MagicIdentity authenticated is {0}", identity.IsAuthenticated));
                Csla.ApplicationContext.User = principal;
                var terms = TermList.GetTermList(new TermCriteria() {
                    PageIndex = 1,
                    PageSize = 50,
                    SearchType = GetTerminologySearchType(searchType), 
                    SearchString = searchString,
                    Domain = (TermDomain)domain
                });

                if(terms != null) {
                    termList = new List<TerminologyInfo1>();
                    foreach(var term in terms) {
                        termList.Add(new TerminologyInfo1() {
                            Code = term.Code,
                            Description = term.Description,
                            IsBillable = term.IsBillable,
                            Modifier1 = term.Modifier1,
                            Modifier2 = term.Modifier2,
                            Modifier3 = term.Modifier3,
                            Modifier4 = term.Modifier4,
                            NDCCode = term.NDCCode,
                            NDCQuantity = term.NDCQuantity,
                            NDCUnitOfMeasure = term.NDCUnitOfMeasure,
                            TerminologyType = (TerminologyTypeEnum)term.TermType,
                            Type = term.Type
                        });
                    }
                }
            }
            return termList;
        }

        private string GetTerminologySearchType(TerminologySearchTypeEnum searchType) {
            var termSearchType = string.Empty;
            switch(searchType) {
                case TerminologySearchTypeEnum.Code:
                    termSearchType = SearchTypes.CodeStartsWith;
                    break;

                case TerminologySearchTypeEnum.CodeSet:
                    termSearchType = SearchTypes.CodeSet;
                    break;

                case TerminologySearchTypeEnum.Description:
                    termSearchType = SearchTypes.DescrContains;
                    break;

                default:
                    break;
            }
            return termSearchType;
        }

    }
}
