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

namespace PlutoServer.PracticeChoice.Core {
    public class Medication {
        public IList<MedicationInfo1> Search(string authToken, string searchString) {
            IList<MedicationInfo1> medsList = null;
            MagicIdentity identity = MagicIdentity.FromCookieString(authToken);
            if(identity.IsAuthenticated) {
                
                var client = new NewCropDrugService.DrugSoapClient("DrugSoap12");
                var drugName = searchString; // At least first 3 letters: acc (for accupril)
                var drugStdType = "F"; // F = First Data Bank, R = RxNorm
                var includeObsolete = "N"; // 'Y' to include obsolete drugs, 'N' otherwise 
                var searchBrandGeneric = "A"; //  'A' for all, 'B' for Brand, 'G' for generic 
                var searchRxOTC = "A"; //  'A' for all, 'R' for Rx (legend drugs), 'O' for Over The Counter drugs 
                var searchDrugSupply = "A"; //  'A' for all, 'D' for Drugs, 'S' for Supplies 
                try {
                    var results = client.DrugSearch(
                        new NewCropDrugService.Credentials() {
                            Name = "demo",
                            PartnerName = "demo",
                            Password = "demo"
                        },
                        new NewCropDrugService.AccountRequest(),
                        new NewCropDrugService.PatientRequest(),
                        new NewCropDrugService.PatientInformationRequester(),
                        drugName,
                        drugStdType,
                        includeObsolete,
                        searchBrandGeneric,
                        searchRxOTC,
                        searchDrugSupply);

                    if((results.result.Status == NewCropDrugService.StatusType.OK) && 
                        (results.drugDetailArray != null) && 
                        (results.drugDetailArray.Count() > 0)) {

                        medsList = new List<MedicationInfo1>();

                        foreach(var drugDetail in results.drugDetailArray) {
                            medsList.Add(new MedicationInfo1() {
                                DataProvider = drugDetail.DataProvider,
                                DEAClassCode = drugDetail.DeaClassCode,
                                Dosage = drugDetail.Dosage,
                                DosageForm = drugDetail.DosageForm,
                                FullName = drugDetail.Drug,
                                GenericName = drugDetail.GenericName,
                                Id = drugDetail.DrugID,
                                Name = drugDetail.DrugName,
                                Route = drugDetail.Route,
                                Status = drugDetail.Status,
                                TherapeuticClass = drugDetail.TherapeuticClass
                            });
                        }
                    }

                } catch(Exception) {

                    throw;
                }

                
            }
            return medsList;
        }
    }
}
