context: using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Text;
  using System.Windows.Forms;
  using OpenDentBusiness;
  
  namespace OpenDental {
  	public partial class FormEhrSettings:Form {
  		private List<string> ListRecEncCodes;
  		private List<string> ListRecPregCodes;
  		private int OldEncListSelectedIdx;
  		private int OldPregListSelectedIdx;
  		private string NewEncCodeSystem;
  		private string NewPregCodeSystem;
  
  		public FormEhrSettings() {
  			InitializeComponent();
  			Lan.F(this);
  		}
  
  		private void FormEhrSettings_Load(object sender,EventArgs e) {
  			if(PrefC.GetString(PrefName.SoftwareName)!="") {
  				this.Text+=" - "+PrefC.GetString(PrefName.SoftwareName);
  			}
  			checkAlertHighSeverity.Checked=PrefC.GetBool(PrefName.EhrRxAlertHighSeverity);
  			checkMU2.Checked=PrefC.GetBool(PrefName.MeaningfulUseTwo);
  			#region DefaultEncounterGroup
  			FillRecEncCodesList();
  			string defaultEncCode=PrefC.GetString(PrefName.CQMDefaultEncounterCodeValue);
  			string defaultEncCodeSystem=PrefC.GetString(PrefName.CQMDefaultEncounterCodeSystem);
  			NewEncCodeSystem=defaultEncCodeSystem;
  			OldEncListSelectedIdx=-1;
  			int countNotInSnomedTable=0;
  			for(int i=0;i<ListRecEncCodes.Count;i++) {
  				if(i==0) {
  					comboEncCodes.Items.Add(ListRecEncCodes[i]);
  					comboEncCodes.SelectedIndex=i;
  					if(defaultEncCode==ListRecEncCodes[i]) {
  						comboEncCodes.SelectedIndex=i;
  						OldEncListSelectedIdx=i;
  					}
  					continue;
  				}
  				if(!Snomeds.CodeExists(ListRecEncCodes[i])) {
  					countNotInSnomedTable++;
  					continue;
  				}
  				comboEncCodes.Items.Add(ListRecEncCodes[i]);
  				if(ListRecEncCodes[i]==defaultEncCode && defaultEncCodeSystem=="SNOMEDCT") {
  					comboEncCodes.SelectedIndex=i;
  					OldEncListSelectedIdx=i;
  					labelEncWarning.Visible=false;	
  					textEncCodeDescript.Text=Snomeds.GetByCode(ListRecEncCodes[i]).Description;//Guaranteed to exist in snomed table from above check
  				}
  			}
  			if(countNotInSnomedTable>0) {
  				MsgBox.Show(this,"The snomed table does not contain one or more codes from the list of recommended encounter codes.  The snomed table should be updated by running the Code System Importer tool found in Setup | Chart | EHR.");
  			}
  			if(comboEncCodes.SelectedIndex==-1) {//default enc code set to code not in recommended list and not 'none'
  				switch(defaultEncCodeSystem) {
  					case "CDT":
  						textEncCodeValue.Text=ProcedureCodes.GetProcCode(defaultEncCode).ProcCode;//Will return a new ProcCode object, not null, if not found
  						textEncCodeDescript.Text=ProcedureCodes.GetProcCode(defaultEncCode).Descript;
  						break;
  					case "CPT":
  						Cpt cEnc=Cpts.GetByCode(defaultEncCode);
  						if(cEnc!=null) {
  							textEncCodeValue.Text=cEnc.CptCode;
  							textEncCodeDescript.Text=cEnc.Description;
  						}
  						break;
  					case "SNOMEDCT":
  						Snomed sEnc=Snomeds.GetByCode(defaultEncCode);
  						if(sEnc!=null) {
  							textEncCodeValue.Text=sEnc.SnomedCode;
  							textEncCodeDescript.Text=sEnc.Description;
  						}						
  						break;
  					case "HCPCS":
  						Hcpcs hEnc=Hcpcses.GetByCode(defaultEncCode);
  						if(hEnc!=null) {
  							textEncCodeValue.Text=hEnc.HcpcsCode;
  							textEncCodeDescript.Text=hEnc.DescriptionShort;
  						}
  						break;
  				}
  			}
  			#endregion
  			#region DefaultPregnancyGroup
  			FillRecPregCodesList();
  			string defaultPregCode=PrefC.GetString(PrefName.PregnancyDefaultCodeValue);
  			string defaultPregCodeSystem=PrefC.GetString(PrefName.PregnancyDefaultCodeSystem);
  			NewPregCodeSystem=defaultPregCodeSystem;
  			OldPregListSelectedIdx=-1;
  			countNotInSnomedTable=0;
  			for(int i=0;i<ListRecPregCodes.Count;i++) {
  				if(i==0) {
  					comboPregCodes.Items.Add(ListRecPregCodes[i]);
  					comboPregCodes.SelectedIndex=i;
  					if(defaultPregCode==ListRecPregCodes[i]) {
  						comboPregCodes.SelectedIndex=i;
  						OldPregListSelectedIdx=i;
  					}
  					continue;
  				}
  				if(!Snomeds.CodeExists(ListRecPregCodes[i])) {
  					countNotInSnomedTable++;
  					continue;
  				}
  				comboPregCodes.Items.Add(ListRecPregCodes[i]);
  				if(ListRecPregCodes[i]==defaultPregCode && defaultPregCodeSystem=="SNOMEDCT") {
  					comboPregCodes.SelectedIndex=i;
  					OldPregListSelectedIdx=i;
  					labelPregWarning.Visible=false;
  					textPregCodeDescript.Text=Snomeds.GetByCode(ListRecPregCodes[i]).Description;//Guaranteed to exist in snomed table from above check
  				}
  			}
  			if(countNotInSnomedTable>0) {
  				MsgBox.Show(this,"The snomed table does not contain one or more codes from the list of recommended pregnancy codes.  The snomed table should be updated by running the Code System Importer tool found in Setup | Chart | EHR.");
  			}
  			if(comboPregCodes.SelectedIndex==-1) {//default preg code set to code not in recommended list and not 'none'
  				switch(defaultPregCodeSystem) {
  					case "ICD9CM":
  						ICD9 i9Preg=ICD9s.GetByCode(defaultPregCode);
  						if(i9Preg!=null) {
  							textPregCodeValue.Text=i9Preg.ICD9Code;
  							textPregCodeDescript.Text=i9Preg.Description;
  						}
  						break;
  					case "SNOMEDCT":
  						Snomed sPreg=Snomeds.GetByCode(defaultPregCode);
  						if(sPreg!=null) {
  							textPregCodeValue.Text=sPreg.SnomedCode;
  							textPregCodeDescript.Text=sPreg.Description;
  						}
  						break;
  					case "ICD10CM":
  						Icd10 i10Preg=Icd10s.GetByCode(defaultPregCode);
  						if(i10Preg!=null) {
  							textPregCodeValue.Text=i10Preg.Icd10Code;
  							textPregCodeDescript.Text=i10Preg.Description;
  						}
  						break;
  					default:
  						break;
  				}
  			}
  			#endregion
  		}
  
  		private void checkAlertHighSeverity_Click(object sender,EventArgs e) {
  			if(!Security.IsAuthorized(Permissions.SecurityAdmin,false)) {
  				checkAlertHighSeverity.Checked=PrefC.GetBool(PrefName.EhrRxAlertHighSeverity);
  			}
  		}
  
  		private void FillRecEncCodesList() {
  			//All of the recommended codes are SNOMEDCT codes
  			ListRecEncCodes=new List<string>();
  			ListRecEncCodes.Add("none");
  			ListRecEncCodes.Add("90526000");//Initial evaluation and management of healthy individual (procedure)
  			ListRecEncCodes.Add("185349003");//Encounter for "check-up" (procedure)
  			ListRecEncCodes.Add("185463005");//Visit out of hours (procedure)
  			ListRecEncCodes.Add("185465003");//Weekend visit (procedure)
  			ListRecEncCodes.Add("270427003");//Patient-initiated encounter (procedure)
  			ListRecEncCodes.Add("270430005");//Provider-initiated encounter (procedure)
  			ListRecEncCodes.Add("308335008");//Patient encounter procedure (procedure)
  			ListRecEncCodes.Add("390906007");//Follow-up encounter (procedure)
  			ListRecEncCodes.Add("406547006");//Urgent follow-up (procedure)
  		}
  
  		private void FillRecPregCodesList() {
  			//All of the recommended codes are SNOMEDCT codes
  			ListRecPregCodes=new List<string>();
  			ListRecPregCodes.Add("none");
  			ListRecPregCodes.Add("72892002");//Normal pregnancy (finding)
  			ListRecPregCodes.Add("77386006");//Patient currently pregnant (finding)
  			ListRecPregCodes.Add("83074005");//Unplanned pregnancy (finding)
  			ListRecPregCodes.Add("169560008");//Pregnant - urine test confirms (finding)
  			ListRecPregCodes.Add("169563005");//Pregnant - on history (finding)
  			ListRecPregCodes.Add("169565003");//Pregnant - planned (finding)
  			ListRecPregCodes.Add("237238006");//Pregnancy with uncertain dates (finding)
  			ListRecPregCodes.Add("248985009");//Presentation of pregnancy (finding)
  			ListRecPregCodes.Add("314204000");//Early stage of pregnancy (finding)
  		}
  
  		private void checkMU2_Click(object sender,EventArgs e) {
  			if(!Security.IsAuthorized(Permissions.SecurityAdmin,false)) {
  				checkMU2.Checked=PrefC.GetBool(PrefName.MeaningfulUseTwo);
  			}
  		}
  
  		private void comboEncCodes_SelectionChangeCommitted(object sender,EventArgs e) {
  			if(!Security.IsAuthorized(Permissions.SecurityAdmin,false)) {
  				comboEncCodes.SelectedIndex=OldEncListSelectedIdx;
  				return;
  			}
  			NewEncCodeSystem="SNOMEDCT";
  			textEncCodeValue.Text="";
  			if(comboEncCodes.SelectedIndex==0) {//none
  				textEncCodeDescript.Clear();
  				labelEncWarning.Visible=true;
  			}
  			else {
  				Snomed sEnc=Snomeds.GetByCode(comboEncCodes.SelectedItem.ToString());
  				if(sEnc==null) {//this check may not be necessary now that we are not adding the code to the list to be selected if they do not have it in the snomed table.  Harmelss and safe.
  					MsgBox.Show(this,"The snomed table does not contain this code.  The code should be added to the snomed table by running the Code System Importer tool.");
  				}
  				else {
  					textEncCodeDescript.Text=sEnc.Description;
  				}
  				labelEncWarning.Visible=false;
  			}
  		}
  
  		private void comboPregCodes_SelectionChangeCommitted(object sender,EventArgs e) {
  			if(!Security.IsAuthorized(Permissions.SecurityAdmin,false)) {
  				comboPregCodes.SelectedIndex=OldPregListSelectedIdx;
  				return;
  			}
  			NewPregCodeSystem="SNOMEDCT";
  			textPregCodeValue.Text="";
  			if(comboPregCodes.SelectedIndex==0) {//none
  				textPregCodeDescript.Clear();
  				labelPregWarning.Visible=true;
  			}
  			else {
  				Snomed sPreg=Snomeds.GetByCode(comboPregCodes.SelectedItem.ToString());
  				if(sPreg==null) {
  					MsgBox.Show(this,"The snomed table does not contain this code.  The code should be added to the snomed table by running the Code System Importer tool.");
  				}
  				else {
  					textPregCodeDescript.Text=sPreg.Description;
  				}
  				labelPregWarning.Visible=false;
  			}
  		}
  
  		private void butEncSnomed_Click(object sender,EventArgs e) {
  			FormSnomeds FormS=new FormSnomeds();
  			if(!Security.IsAuthorized(Permissions.SecurityAdmin,false)) {
  				FormS.IsSelectionMode=false;
  			}
  			else {
  				FormS.IsSelectionMode=true;
  			}
  			FormS.ShowDialog();
  			if(FormS.DialogResult==DialogResult.OK) {
  				NewEncCodeSystem="SNOMEDCT";
  
gt: 				for(int i=1;i<comboEncCodes.Items.Count;i++) {