using System;
using System.Collections.Generic;
using System.ComponentModel;
using CLIP.eForm.Consent.UI.HospitalSvcRef;
using System.Windows.Forms;

namespace CLIP.eForm.Consent.UI {
  /// <summary>
  /// 환자목록 클래스
  /// </summary>
  /// <remarks>
  /// <p>[설계자]</p>
  /// <p>  클립소프트 연구소 홍지철 (jchong@clipsoft.co.kr)</p>
  /// <p>[원본 작성자]</p>
  /// <p>  클립소프트 기술부 이인희</p>
  /// <p>[수정 작성자]</p>
  /// <p>  </p>
  /// <p>----------------------------------------------------------------------------------------</p>
  /// <p>[HISTORY]</p>
  /// <p> 2016-06-21 : 최초작성</p>
  /// <p>----------------------------------------------------------------------------------------</p>
  /// </remarks>  
  public partial class PatientListCtrl : PatientListCtrlBase {
    private IConsentMain consentMain = null;

    public PatientListCtrl() {
      InitializeComponent();
    }

    private void PatientListCtrl_Load(object sender, EventArgs e) {
      if(this.DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime) {
        return;
      }

      consentMain = ConsentMainControl.GetConsentMainInterface(this);
      consentMain.OnLoadPartControls += ConsentMain_OnLoadPartControls;
    }

    private void ConsentMain_OnLoadPartControls(object sender, EventArgs e) {
      if(this.DesignMode) {
        return;
      }

      IConsentMain consentMain = ConsentMainControl.GetConsentMainInterface(this);

      // Ku2.0 연동 시에는 이벤트 태우지 않음
      if(consentMain.ConsentExecuteInfo["printYN"].Equals("Y")) {
        return;
      }

      pageMove_tabPageOutPatient(consentMain);
      SetPatientListSplitPanelVisibility(consentMain);
    }

    /// <summary>
    /// patientListView 의 값에 따라 PatientList 패널 활성화 여부 설정
    /// </summary>
    /// <param name="consentMain">patientListView 를 가지고 있는 consentMain 인스턴스</param>
    private static void SetPatientListSplitPanelVisibility(IConsentMain consentMain) {
      if(!(string.IsNullOrEmpty(consentMain.ConsentExecuteInfo["patientListView"])) && consentMain.ConsentExecuteInfo["patientListView"].Equals("Y")) {
        consentMain.SetPatientListSplitPanelVisibility(true);
      }
      else {
        consentMain.SetPatientListSplitPanelVisibility(false);
      }
    }

    /// <summary>
    /// 탭 변경 이벤트
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
    private void tabControlPatientSelect_SelectedIndexChanged(object sender, EventArgs e) {
      // 탭 선택을 할때마다 선택된 체크박스는 초기화 한다.
      tabPageClearCheckBox(tabControlPatientSelect.SelectedIndex);
    }

    public int getSelectedIndex() {
      return tabControlPatientSelect.SelectedIndex;
    }

    /// <summary>
    /// 탭 페이지 내의 체크박스 초기화
    /// </summary>
    /// <param name="tabIndex">현재 선택된 탭 페이지</param>
    private void tabPageClearCheckBox(int tabIndex) {
      if(tabIndex == 0) {
        if(this.tabControlPatientSelect.TabPages["tabPageSearchPatient"].Controls.Count > 0) {
          PatientSelectTabSearchPatient searchPatient = this.tabControlPatientSelect.TabPages["tabPageSearchPatient"].Controls[0] as PatientSelectTabSearchPatient;
          searchPatient.ClearCheckBox();
        }
      }
      else if(tabIndex == 1) {
        if(this.tabControlPatientSelect.TabPages["tabPageEmergencyPatient"].Controls.Count > 0) {
          PatientSelectTabEmergencyPatient searchPatient = this.tabControlPatientSelect.TabPages["tabPageEmergencyPatient"].Controls[0] as PatientSelectTabEmergencyPatient;
          searchPatient.ClearCheckBox();
        }
      }
      else if(tabIndex == 2) {
        if(this.tabControlPatientSelect.TabPages["tabPageOperatePatient"].Controls.Count > 0) {
          PatientSelectTabOperatePatient searchPatient = this.tabControlPatientSelect.TabPages["tabPageOperatePatient"].Controls[0] as PatientSelectTabOperatePatient;
          searchPatient.ClearCheckBox();
        }
      }
      else if(tabIndex == 3) {
        if(this.tabControlPatientSelect.TabPages["tabPageInPatient"].Controls.Count > 0) {
          PatientSelectTabInPatient searchPatient = this.tabControlPatientSelect.TabPages["tabPageInPatient"].Controls[0] as PatientSelectTabInPatient;
          searchPatient.ClearCheckBox();
        }
      }
      else if(tabIndex == 4) {
        if(this.tabControlPatientSelect.TabPages["tabPageOutPatient"].Controls.Count > 0) {
          PatientSelectTabOutPatient searchPatient = this.tabControlPatientSelect.TabPages["tabPageOutPatient"].Controls[0] as PatientSelectTabOutPatient;
          searchPatient.ClearCheckBox();
        }
      }
    }

    /// <summary>
    /// 클래스 외부에서 tabPageClearCheckBox 를 호출하기 위한 메서드
    /// </summary>
    public override void SetClearCheckBox() {
      // 선택된 탭의 그리드 체크박스를 해제한다.
      tabPageClearCheckBox(tabControlPatientSelect.SelectedIndex);
    }

    /// <summary>
    /// 탭의 그리드에서 더블클릭하여 선택한 환자정보로 동의서 관리를 한다.
    /// </summary>
    /// <param name="vo">The vo.</param>
    public override void SetChangePID(PatListVO vo) {
      
      if(vo != null) {
        // TODO..
        // 동의서가 view일 경우
        if(consentMain.viewStatus && !consentMain.IsComplete()) {
          // dbs227, 임시저장 팝업 사용 안함
          //DialogResult result = MessageBox.Show(string.Format(Properties.Resources.msg_confirm_tempsave)
          //                        , string.Format(Properties.Resources.msg_caption_confirm),
          //                        MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);

          //if (result == DialogResult.Yes) {
          //    consentMain.TempSave();
          //} else if (result == DialogResult.Cancel) {
          //    return;
          //}
        }

        var curIdx = tabControlPatientSelect.SelectedIndex;

        if(curIdx == 2) {
          consentMain.setOpPatientSelected(true);
        }
        else {
          consentMain.setOpPatientSelected(false);
        }

        consentMain.ClearPreviewConsent(false);

        consentMain.ConsentExecuteInfo["patientNo"] = vo.pid;
        consentMain.ConsentExecuteInfo["clnDate"] = vo.inDd.Replace("/", "").Replace("-", "");
        consentMain.ConsentExecuteInfo["clnDept"] = vo.ordDeptCd;
        consentMain.ConsentExecuteInfo["visitType"] = vo.ordType;
        consentMain.ConsentExecuteInfo["cretno"] = vo.cretNo.ToString();
        consentMain.ConsentExecuteInfo["userNo"] = consentMain.ConsentExecuteInfo["loginUserNo"];
        consentMain.ConsentExecuteInfo["userName"] = consentMain.ConsentExecuteInfo["loginUserName"];
        consentMain.ConsentExecuteInfo["opRsrvNo"] = vo.oprsRvNo;

        consentMain.changeInDd(consentMain.ConsentExecuteInfo["clnDate"]);

        // 환자 선택 시 동의서 이력 재조회 루틴 추가
        Cursor currentCursor = this.Cursor;
        this.Cursor = Cursors.WaitCursor;
        consentMain.UIControlsRefeash(vo.ordType);
        this.Cursor = currentCursor;
      }
    }

    /// <summary>
    /// 탭의 그리드에서 체크된 환자리스트를 갖고 온다.
    /// </summary>
    /// <returns></returns>
    public override List<PatListVO> GetSelectedPatientList() {

      List<PatListVO> patList = null;

      if(tabControlPatientSelect.SelectedIndex == 0) {
        if(this.tabControlPatientSelect.TabPages["tabPageSearchPatient"].Controls.Count > 0) {
          PatientSelectTabSearchPatient searchPatient = this.tabControlPatientSelect.TabPages["tabPageSearchPatient"].Controls[0] as PatientSelectTabSearchPatient;
          patList = searchPatient.GetCheckPatList();
        }
      }
      else if(tabControlPatientSelect.SelectedIndex == 1) {
        if(this.tabControlPatientSelect.TabPages["tabPageEmergencyPatient"].Controls.Count > 0) {
          PatientSelectTabEmergencyPatient searchPatient = this.tabControlPatientSelect.TabPages["tabPageEmergencyPatient"].Controls[0] as PatientSelectTabEmergencyPatient;
          patList = searchPatient.GetCheckPatList();
        }
      }
      else if(tabControlPatientSelect.SelectedIndex == 2) {
        if(this.tabControlPatientSelect.TabPages["tabPageOperatePatient"].Controls.Count > 0) {
          PatientSelectTabOperatePatient searchPatient = this.tabControlPatientSelect.TabPages["tabPageOperatePatient"].Controls[0] as PatientSelectTabOperatePatient;
          patList = searchPatient.GetCheckPatList();
        }
      }
      else if(tabControlPatientSelect.SelectedIndex == 3) {
        if(this.tabControlPatientSelect.TabPages["tabPageInPatient"].Controls.Count > 0) {
          PatientSelectTabInPatient searchPatient = this.tabControlPatientSelect.TabPages["tabPageInPatient"].Controls[0] as PatientSelectTabInPatient;
          patList = searchPatient.GetCheckPatList();
        }
      }
      else if(tabControlPatientSelect.SelectedIndex == 4) {
        if(this.tabControlPatientSelect.TabPages["tabPageOutPatient"].Controls.Count > 0) {
          PatientSelectTabOutPatient searchPatient = this.tabControlPatientSelect.TabPages["tabPageOutPatient"].Controls[0] as PatientSelectTabOutPatient;
          patList = searchPatient.GetCheckPatList();
        }
      }

      return patList;
    }

    /// <summary>
    /// 외래 탭으로 이동
    /// </summary>
    /// <param name="consentMain">The consent main.</param>
    public override void pageMove_tabPageOutPatient(IConsentMain consentMain) {
      if(consentMain.ConsentExecuteInfo["userIOFlag"].Equals("I")) {
        this.tabControlPatientSelect.SelectedTab = this.tabControlPatientSelect.TabPages["tabPageInPatient"];
      }
      else {
        this.tabControlPatientSelect.SelectedTab = this.tabControlPatientSelect.TabPages["tabPageOutPatient"];
      }
    }
  }
}
