package com.dbs.mplus.knuh.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.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.dbs.mplus.knuh.BuildConfig;
import com.dbs.mplus.knuh.R;
import com.dbs.mplus.knuh.activity.activityEvent.LeftMenu;
import com.dbs.mplus.knuh.activity.activityEvent.RightMenu;
import com.dbs.mplus.knuh.activity.activityEvent.TapMenu;
import com.dbs.mplus.knuh.activity.activityEvent.TopMenu;
import com.dbs.mplus.knuh.adapter.ViewPagerAdapter;
import com.dbs.mplus.knuh.consent.sign.SignWrapper;
import com.dbs.mplus.knuh.customView.CustomAlertDialog;
import com.dbs.mplus.knuh.customView.CustomRadioGroup;
import com.dbs.mplus.knuh.customView.LoadingProgress;
import com.dbs.mplus.knuh.fragment.HistoryFragment;
import com.dbs.mplus.knuh.fragment.MarkerFragment;
import com.dbs.mplus.knuh.fragment.SearchFragment;
import com.dbs.mplus.knuh.util.ConsentConfig;
import com.dbs.mplus.knuh.util.Util;

public class ConsentActivity extends AppCompatActivity {

  private static final String TAG = "ConsentActivity";
  public AppCompatActivity mActivity;
  public Context mContext;
  private LoadingProgress loadingProgress;
  public CustomAlertDialog csAlert;
  private TopMenu topMenu;
  private TapMenu tapMenu;
  private LeftMenu leftMenu;
  private RightMenu rightMenu;

  /** 상단 메뉴 **/
  private ImageView ivLogo;
  private TextView tvEdu;
  private TextView tvConsent;
  private TextView tvDoctor;
  private TextView tvDept;
  private LinearLayout settingLayout;
  private LinearLayout logoutLayout;
  /** 상단 메뉴 **/
  /** 탭 메뉴 **/
  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;
  /** 탭 메뉴 **/
  /** left Menu **/
  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;
  /** left Menu **/

  /** 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 **/

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

  public String deptCd    = "";
  public String deptNm    = "";
  public String jobKindCd = "";
  public String jobKindNm = "";
  public String userId    = "";
  public String userName   = "";
  public String indexPage = "";
  public String drKind    = "";

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

    init();
  }

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

    getIntentData();


    setTopMenuLayout();
    setTapMenuLayout();
    setLeftLayout();

    setRightLayout();

    setTopMenuEvent();
  }

  private void getIntentData() {
    Intent intent = getIntent();
    deptCd     = intent.getStringExtra("deptCd");
    deptNm     = intent.getStringExtra("deptNm");
    jobKindCd  = intent.getStringExtra("jobKindCd");
    jobKindNm  = intent.getStringExtra("jobKindNm");
    userId     = intent.getStringExtra("userId");
    userName   = intent.getStringExtra("userName");
    indexPage  = intent.getStringExtra("indexPage");
    drKind     = intent.getStringExtra("drKind");

    Log.e(TAG, "deptCd    -- > " + deptCd   );
    Log.e(TAG, "deptNm    -- > " + deptNm   );
    Log.e(TAG, "jobKindCd -- > " + jobKindCd);
    Log.e(TAG, "jobKindNm -- > " + jobKindNm);
    Log.e(TAG, "userId    -- > " + userId   );
    Log.e(TAG, "userName  -- > " + userName );
    Log.e(TAG, "indexPage -- > " + indexPage);
    Log.e(TAG, "drKind    -- > " + drKind   );

    setLoadClass();
  }

  private void setLoadClass() {
    loadingProgress = new LoadingProgress(mContext);
    csAlert = new CustomAlertDialog(mContext);
    topMenu  = new TopMenu(mContext, mActivity);
    tapMenu  = new TapMenu(mContext, mActivity);
    leftMenu = new LeftMenu(mContext, mActivity);
    rightMenu = new RightMenu(mContext, mActivity);
  }

  private void setTopMenuLayout() {
    ivLogo        = (ImageView) findViewById(R.id.ivLogo);
    tvEdu         = (TextView) findViewById(R.id.tvEdu);
    tvConsent     = (TextView) findViewById(R.id.tvConsent);
    tvDoctor      = (TextView) findViewById(R.id.tvDoctor);
    tvDept        = (TextView) findViewById(R.id.tvDept);
    settingLayout = (LinearLayout) findViewById(R.id.settingLayout);
    logoutLayout  = (LinearLayout) findViewById(R.id.logoutLayout);


    int logo = 0;
    if (BuildConfig.INST_CD.equals("031")) {
      logo = R.drawable.knuh_logo;
    } else {
      logo = R.drawable.chilgok_logo;
    }

    if (Util.getAppName(mContext).indexOf(getString(R.string.edu)) > 0) {
      tvEdu.setVisibility(View.VISIBLE);
      tvEdu.setText(Util.getAppName(mContext));
    } else {
      tvEdu.setVisibility(View.GONE);
    }

    ivLogo.setImageResource(logo);
    tvDoctor.setText(userName);
    tvDept.setText(deptNm);
    Drawable tvEduBg = (Drawable) tvEdu.getBackground();
    tvEduBg.setColorFilter(ContextCompat.getColor(mContext, R.color.eduColor), PorterDuff.Mode.SRC_ATOP);
    Drawable tvConsentBg = (Drawable) tvConsent.getBackground();
    tvConsentBg.setColorFilter(ContextCompat.getColor(mContext, R.color.consentColor), PorterDuff.Mode.SRC_ATOP);
  }

  private void setTopMenuEvent() {
    settingLayout.setOnClickListener(topMenu.settingClickListener);
    logoutLayout.setOnClickListener(topMenu.logoutClickListener);
  }

  private void setTapMenuLayout() {
    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(indexPage);
    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);
//    radioGroup    = (CustomRadioGroup) findViewById(R.id.radioGroup);
//    radioBtn1     = (RadioButton) findViewById(R.id.radioBtn1);
//    radioBtn2     = (RadioButton) findViewById(R.id.radioBtn2);
//    radioBtn3     = (RadioButton) findViewById(R.id.radioBtn3);
//    radioBtn4     = (RadioButton) findViewById(R.id.radioBtn4);
//    radioBtn5     = (RadioButton) findViewById(R.id.radioBtn5);
    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);

    leftMenu.setRadioGroupDraw(indexPage);

    if (!indexPage.equals("I")) {
      wardLayout.setVisibility(View.INVISIBLE);
      chkMyPatient.setVisibility(View.GONE);
    }

    tvMainDate.setText(Util.nowDate());

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

    leftMenu.getWardList();
    leftMenu.getDeptList();
  }

  public 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();
  }

  public void setViewPager() {
    ViewPagerAdapter agreeAdapter = new ViewPagerAdapter(getSupportFragmentManager());

    final HistoryFragment history = new HistoryFragment();
    agreeAdapter.addItem(history);

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

    SearchFragment 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
  protected void onDestroy() {
    if (tapClick == false) {
      try {
//        SignWrapper.getInstance().deleteAll();
//        Util.clearPreference(mContext, "userInfo");
//        Util.clearPreference(mContext, "sessionTime");
      } catch (Exception e) {
      }
    }

    super.onDestroy();
  }
}
