package com.dbs.mplus.fatima.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatRadioButton;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.dbs.mplus.fatima.R;
import com.dbs.mplus.fatima.activity.activityEvent.CenterMenu;
import com.dbs.mplus.fatima.activity.activityEvent.LeftMenu;
import com.dbs.mplus.fatima.activity.activityEvent.RightMenu;
import com.dbs.mplus.fatima.activity.activityEvent.TapMenu;
import com.dbs.mplus.fatima.activity.activityEvent.TopMenu;
import com.dbs.mplus.fatima.adapter.ConsentHistoryAdapter;
import com.dbs.mplus.fatima.adapter.ConsentTempAdapter;
import com.dbs.mplus.fatima.adapter.ConsentWriteAdapter;
import com.dbs.mplus.fatima.adapter.ViewPagerAdapter;
import com.dbs.mplus.fatima.customView.CustomAlertDialog;
import com.dbs.mplus.fatima.customView.CustomRadioGroup;
import com.dbs.mplus.fatima.customView.LoadingProgress;
import com.dbs.mplus.fatima.fragment.HistoryFragment;
import com.dbs.mplus.fatima.fragment.MarkerFragment;
import com.dbs.mplus.fatima.fragment.SearchFragment;
import com.dbs.mplus.fatima.util.ConsentConfig;
import com.dbs.mplus.fatima.util.Util;

public class ConsentActivity extends AppCompatActivity {

  private static final String TAG = "ConsentActivity";
  private AppCompatActivity mActivity;
  private Context mContext;
  public CustomAlertDialog csAlert;
  private LoadingProgress loadingProgress;

  private TopMenu topMenu;
  private TapMenu tapMenu;
  private LeftMenu leftMenu;
  public CenterMenu centerMenu;
  private RightMenu rightMenu;

  public HistoryFragment history;
  public MarkerFragment marker;
  public SearchFragment search;
  public ConsentTempAdapter consentCenterAdaper;
  public ConsentHistoryAdapter consentHistoryAdapter;
  public ConsentWriteAdapter consentWriteAdapter;

  private long backKeyPressedTime = 0;
  public boolean tapClick = false;

  public String emplNo        = "";
  public String name          = "";
  public String unitNm        = "";
  public String dutplcedeptCd = "";
  public String dutplceDeptNm = "";
  public String jobKindCd     = "";
  public String jobKindNm     = "";
  public String mobile        = "";

  // ----------------- top --------------------------
  private TextView tvDept;
  private TextView tvDoctor;
  public LinearLayout settingLayout;
  public LinearLayout logoutLayout;
  private TextView tvConsent;
  // ----------------- top --------------------------

  // ----------------- tap --------------------------
  public LinearLayout tabAdmission;
  public ImageView ivInPatient;
  public TextView tvInPatient;
  public LinearLayout tabOutPatient;
  public ImageView ivOutPatient;
  public TextView tvOutPatient;
  public LinearLayout tabEmergency;
  public ImageView ivErPatient;
  public TextView tvErPatient;
  public LinearLayout tabSurgery;
  public ImageView ivOpPatient;
  public TextView tvOpPatient;
  public LinearLayout tabSearch;
  public ImageView ivSearchPatient;
  public TextView tvSearchPatient;
  // ----------------- tap --------------------------

  // ----------------- left --------------------------
  public LinearLayout dateLayout;
  public TextView tvMainDate;
  public LinearLayout wardLayout;
  public Spinner wardSpinner;
  public LinearLayout deptLayout;
  public Spinner deptSpinner;
  public LinearLayout doctorLayout;
  public Spinner doctorSpinner;
  //  public CustomRadioGroup radioGroup;
//  public RadioButton radioBtn1;
//  public RadioButton radioBtn2;
//  public RadioButton radioBtn3;
//  public RadioButton radioBtn4;
//  public RadioButton radioBtn5;
  public RelativeLayout radioLayout;
  public CustomRadioGroup radioGroup;
  public CustomRadioGroup.LayoutParams radioGroupParams;
  public RelativeLayout.LayoutParams radioParams;
  public AppCompatRadioButton radioButton1;
  public AppCompatRadioButton radioButton2;
  public AppCompatRadioButton radioButton3;
  public AppCompatRadioButton radioButton4;
  public AppCompatRadioButton radioButton5;

  public EditText etPatientId;
  public LinearLayout patientSearchLayout;

  public RecyclerView patientRecyclerView;
  public TextView patientEmpty;
  public CheckBox chkMyPatient;
  public LinearLayout llPatientSort;
  public LinearLayout llPatientNmSort;
  public LinearLayout llRoomSort;
  public TextView column1;
  public TextView column3;
  public TextView column4;
  public TextView column5;
  public TextView column6;
  // ----------------- left --------------------------

  /** Center Menu **/
  public TextView tvPatientInfo;
  public TextView tvAdmissionDay;
  public TextView tvChargeDoctor;
  public TextView tvDiagnosisName;
  public TextView tvPatientAlert;
  public TextView tvTempCenterTxt;
  public RecyclerView tempRecyclerView;
  /** Center Meny **/

  /** Right Menu **/
  public LinearLayout historyLayout;
  public LinearLayout markerLayout;
  public LinearLayout searchLayout;

  public TextView     tvRightHistory;
  public TextView     tvRightMarker;
  public TextView     tvRightSearch;
  public ViewPager viewPager;
  /** Right Menu **/


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_consent);

    init();
  }

  private void init() {
    mActivity = this;
    mContext  = this;

    loadingProgress = new LoadingProgress(mContext);
    topMenu = new TopMenu(mActivity, mContext);
    tapMenu = new TapMenu(mActivity, mContext);
    leftMenu = new LeftMenu(mActivity, mContext);
    centerMenu = new CenterMenu(mActivity, mContext);
    rightMenu = new RightMenu(mContext, mActivity);
    csAlert = new CustomAlertDialog(mContext);

    getDefaultData();
    setTopLayout();
    setTapLayout();
    setLeftLayout();
    setCenterLayout();
    setRightLayout();
  }

  private void getDefaultData() {
    Intent intent = getIntent();
    emplNo        = intent.getStringExtra("emplNo");
    name          = intent.getStringExtra("name");
    unitNm        = intent.getStringExtra("unitNm");
    dutplcedeptCd = intent.getStringExtra("dutplcedeptCd");
    dutplceDeptNm = intent.getStringExtra("dutplceDeptNm");
    jobKindCd     = intent.getStringExtra("jobKindCd");
    jobKindNm     = intent.getStringExtra("jobKindNm");
    mobile        = intent.getStringExtra("mobile");

    Log.e(TAG, "emplNo       -- > " + emplNo       );
    Log.e(TAG, "name         -- > " + name         );
    Log.e(TAG, "unitNm       -- > " + unitNm       );
    Log.e(TAG, "dutplcedeptCd-- > " + dutplcedeptCd       );
    Log.e(TAG, "dutplceDeptNm-- > " + dutplceDeptNm);
    Log.e(TAG, "jobKindCd    -- > " + jobKindCd    );
    Log.e(TAG, "jobKindNm    -- > " + jobKindNm    );
    Log.e(TAG, "mobile       -- > " + mobile       );

  }

  private void setTopLayout() {
    tvDept = (TextView) findViewById(R.id.tvDept);
    tvDoctor = (TextView) findViewById(R.id.tvDoctor);

    settingLayout = (LinearLayout) findViewById(R.id.settingLayout);
    logoutLayout = (LinearLayout) findViewById(R.id.logoutLayout);
    tvConsent = (TextView) findViewById(R.id.tvConsent);

    tvDept.setText(dutplceDeptNm);
    tvDoctor.setText(name);

    Drawable tvConsentBg = (Drawable) tvConsent.getBackground();
    tvConsentBg.setColorFilter(ContextCompat.getColor(mContext, R.color.consentColor), PorterDuff.Mode.SRC_ATOP);

    topMenu.setEvent();
  }

  private void setTapLayout() {
    tabAdmission    = (LinearLayout) findViewById(R.id.tabAdmission);
    ivInPatient     = (ImageView) findViewById(R.id.ivInPatient);
    tvInPatient     = (TextView) findViewById(R.id.tvInPatient);
    tabOutPatient   = (LinearLayout) findViewById(R.id.tabOutPatient);
    ivOutPatient    = (ImageView) findViewById(R.id.ivOutPatient);
    tvOutPatient    = (TextView) findViewById(R.id.tvOutPatient);
    tabEmergency    = (LinearLayout) findViewById(R.id.tabEmergency);
    ivErPatient     = (ImageView) findViewById(R.id.ivErPatient);
    tvErPatient     = (TextView) findViewById(R.id.tvErPatient);
    tabSurgery      = (LinearLayout) findViewById(R.id.tabSurgery);
    ivOpPatient     = (ImageView) findViewById(R.id.ivOpPatient);
    tvOpPatient     = (TextView) findViewById(R.id.tvOpPatient);
    tabSearch       = (LinearLayout) findViewById(R.id.tabSearch);
    ivSearchPatient = (ImageView) findViewById(R.id.ivSearchPatient);
    tvSearchPatient = (TextView) findViewById(R.id.tvSearchPatient);

    tapMenu.tapMenuColorChange(mobile);
    tabAdmission.setOnClickListener(tapMenu.onClickListener);
    tabOutPatient.setOnClickListener(tapMenu.onClickListener);
    tabEmergency.setOnClickListener(tapMenu.onClickListener);
    tabSurgery.setOnClickListener(tapMenu.onClickListener);
    tabSearch.setOnClickListener(tapMenu.onClickListener);
  }

  private void setLeftLayout() {
    dateLayout    = (LinearLayout) findViewById(R.id.dateLayout);
    tvMainDate    = (TextView) findViewById(R.id.tvMainDate);
    wardLayout    = (LinearLayout) findViewById(R.id.wardLayout);
    wardSpinner   = (Spinner) findViewById(R.id.wardSpinner);
    deptLayout    = (LinearLayout) findViewById(R.id.deptLayout);
    deptSpinner   = (Spinner) findViewById(R.id.deptSpinner);
    doctorLayout  = (LinearLayout) findViewById(R.id.doctorLayout);
    doctorSpinner = (Spinner) findViewById(R.id.doctorSpinner);
    radioLayout = (RelativeLayout) findViewById(R.id.radioLayout);

    etPatientId = (EditText) findViewById(R.id.etPatientId);
    patientSearchLayout = (LinearLayout) findViewById(R.id.patientSearchLayout);

    patientRecyclerView = (RecyclerView) findViewById(R.id.patientRecyclerView);
    patientEmpty = (TextView) findViewById(R.id.patientEmpty);
    chkMyPatient = (CheckBox) findViewById(R.id.chkMyPatient);

    llPatientSort = (LinearLayout) findViewById(R.id.llPatientSort);
    llPatientNmSort = (LinearLayout) findViewById(R.id.llPatientNmSort);
    llRoomSort = (LinearLayout) findViewById(R.id.llRoomSort);

    column1 = (TextView) findViewById(R.id.column1);
    column3 = (TextView) findViewById(R.id.column3);
    column4 = (TextView) findViewById(R.id.column4);
    column5 = (TextView) findViewById(R.id.column5);
    column6 = (TextView) findViewById(R.id.column6);

    if (!mobile.equals("I")) {
      wardLayout.setVisibility(View.INVISIBLE);
    } else {
      wardLayout.setVisibility(View.VISIBLE);
    }

    if (mobile.equals("I")) {
      column6.setText(getString(R.string.wardRoom));
    } else if (mobile.equals("O")) {
      column6.setText("");
    } else if (mobile.equals("E")) {
      column6.setText(getString(R.string.patientLocation));
    } else if (mobile.equals("P")) {
      column3.setText(getString(R.string.opDept));
      column5.setText(getString(R.string.opDate));
      column6.setText(getString(R.string.opRoom));
    } else if (mobile.equals("SR")) {
      column6.setText(getString(R.string.adDate));
    }

    tvMainDate.setText(Util.nowDate());
    leftMenu.setRadioGroupDraw(mobile);

    dateLayout.setOnClickListener(leftMenu.onClickListener);
    radioGroup.setOnCheckedChangeListener(leftMenu.onCheckedChangeListener);
    patientSearchLayout.setOnClickListener(leftMenu.onClickListener);
    etPatientId.setOnEditorActionListener(leftMenu.onEditorActionListener);

    leftMenu.getWardList();
    leftMenu.getDeptList();
    if (!mobile.equals("SR")) {
      leftMenu.getInitPatientList();
    }

  }

  private void setCenterLayout() {
    tvPatientInfo    = (TextView) findViewById(R.id.tvPatientInfo);
    tvAdmissionDay   = (TextView) findViewById(R.id.tvAdmissionDay);
    tvChargeDoctor   = (TextView) findViewById(R.id.tvChargeDoctor);
    tvDiagnosisName  = (TextView) findViewById(R.id.tvDiagnosisName);
    tvPatientAlert   = (TextView) findViewById(R.id.tvPatientAlert);
    tvTempCenterTxt  = (TextView) findViewById(R.id.tvTempCenterTxt);
    tempRecyclerView = (RecyclerView) findViewById(R.id.tempRecyclerView);
  }

  private void setRightLayout() {
    /** RightTab **/
    historyLayout  = (LinearLayout) findViewById(R.id.historyLayout);
    markerLayout   = (LinearLayout) findViewById(R.id.markerLayout);
    searchLayout   = (LinearLayout) findViewById(R.id.searchLayout);
    tvRightHistory = (TextView) findViewById(R.id.tvHistory);
    tvRightMarker  = (TextView) findViewById(R.id.tvMarker);
    tvRightSearch  = (TextView) findViewById(R.id.tvSearch);
    viewPager      = (ViewPager) findViewById(R.id.viewPager);
    /** RightTab **/

    historyLayout.setOnClickListener(rightMenu.onClickListener);
    markerLayout.setOnClickListener(rightMenu.onClickListener);
    searchLayout.setOnClickListener(rightMenu.onClickListener);

    setViewPager();
    rightMenu.rightMenuStartColor();
  }

  private void setViewPager() {
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);;
    ViewPagerAdapter agreeAdapter = new ViewPagerAdapter(getSupportFragmentManager());
//    history = new HistoryFragment(mActivity, mContext, centerMenu);
    history = new HistoryFragment(mActivity, mContext, centerMenu);
    agreeAdapter.addItem(history);

    marker = new MarkerFragment(mActivity, mContext);
    agreeAdapter.addItem(marker);

    search = new SearchFragment(mActivity, mContext);
    agreeAdapter.addItem(search);

    viewPager.setAdapter(agreeAdapter);
    viewPager.setOffscreenPageLimit(2);
    viewPager.setCurrentItem(1);
//    startTabColor();
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int i, float v, int i1) {
      }

      @Override
      public void onPageSelected(int position) {
        Log.e(TAG, "i -- > " + position);
//        changedColorTab(position);
        rightMenu.rightMenuColorChange(position);
        if (position == 1) {
          marker.getConsentSetList();
        }
      }

      @Override
      public void onPageScrollStateChanged(int i) {
      }
    });
  }

  public void showSingButtonDialog(String message) {
    csAlert.show();
    csAlert.setContent(message, ConsentConfig.ALERT_BTN_TYPE);
    csAlert.btnConfirm2.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        csAlert.dismiss();
      }
    });
  }

  public void showProgress() {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        if (!loadingProgress.isShowing()) {
          loadingProgress.show();
        }
      }
    });

  }

  public void dismissProgress() {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        if (loadingProgress.isShowing()) {
          loadingProgress.dismiss();
        }
      }
    });

  }

  @Override
  public void onBackPressed() {
    if(System.currentTimeMillis() > backKeyPressedTime + 2000) {
      backKeyPressedTime = System.currentTimeMillis();
      Toast.makeText(mContext, getString(R.string.doublePressed), Toast.LENGTH_SHORT).show();
      return;
    }

    if(System.currentTimeMillis() <= backKeyPressedTime + 2000) {
      tapClick = false;
//      super.onBackPressed();
      finishAffinity();
    }
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
  }

}
