package com.dbs.mplus.knuh.fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.app.DatePickerDialog;
import android.content.Context;

import android.graphics.PorterDuff;
import android.graphics.drawable.GradientDrawable;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.dbs.mplus.knuh.R;
import com.dbs.mplus.knuh.customView.CustomAlertDialog;
import com.dbs.mplus.knuh.util.ConsentConfig;
import com.dbs.mplus.knuh.util.Util;


public class HistoryFragment extends Fragment {

  private static final String TAG = "HistoryFragment";
  private AppCompatActivity mActiyivy;
  private Context mContext;

  private DatePickerDialog leftDateDialog;
  private DatePickerDialog rightDateDialog;
  private CustomAlertDialog csAlert;

  private LinearLayout typePatient;
  private LinearLayout patientLeftRound;
  private ImageView ivPatient;
  private LinearLayout patientRightRound;
  private TextView tvPatient;

  private LinearLayout typeWriter;
  private LinearLayout writerLeftRound;
  private ImageView ivWriter;
  private LinearLayout writerRightRound;
  private TextView tvWriter;

  private Button btnHistorySearch;

  private LinearLayout leftDateLayout;
  private LinearLayout rightDateLayout;
  private TextView tvLeftDate;
  private TextView tvRightDate;

  private CheckBox checkAll;
  private CheckBox checkTemp;
  private CheckBox checkCert;
  private CheckBox checkConfirm;
  private CheckBox checkPrint;

  public HistoryFragment() {

  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mContext = getActivity();
    mActiyivy = (AppCompatActivity) getActivity();
  }

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = (View) inflater.inflate(R.layout.activity_history_fragment, container, false);

    csAlert = new CustomAlertDialog(mContext);

    typePatient       = (LinearLayout) view.findViewById(R.id.typePatient);
    patientLeftRound  = (LinearLayout) view.findViewById(R.id.patientLeftRound);
    ivPatient         = (ImageView) view.findViewById(R.id.ivPatient);
    patientRightRound = (LinearLayout) view.findViewById(R.id.patientRightRound);
    tvPatient         = (TextView) view.findViewById(R.id.tvPatient);
    typeWriter        = (LinearLayout) view.findViewById(R.id.typeWriter);
    writerLeftRound   = (LinearLayout) view.findViewById(R.id.writerLeftRound);
    ivWriter          = (ImageView) view.findViewById(R.id.ivWriter);
    writerRightRound  = (LinearLayout) view.findViewById(R.id.writerRightRound);
    tvWriter          = (TextView) view.findViewById(R.id.tvWriter);
    btnHistorySearch  = (Button) view.findViewById(R.id.btnHistorySearch);

    leftDateLayout  = (LinearLayout) view.findViewById(R.id.leftDateLayout);
    rightDateLayout = (LinearLayout) view.findViewById(R.id.rightDateLayout);
    tvLeftDate      = (TextView) view.findViewById(R.id.tvLeftDate);
    tvRightDate     = (TextView) view.findViewById(R.id.tvRightDate);

    checkAll     = (CheckBox) view.findViewById(R.id.checkAll);
    checkTemp    = (CheckBox) view.findViewById(R.id.checkTemp);
    checkCert    = (CheckBox) view.findViewById(R.id.checkCert);
    checkConfirm = (CheckBox) view.findViewById(R.id.checkConfirm);
    checkPrint   = (CheckBox) view.findViewById(R.id.checkPrint);

    GradientDrawable btnSearchBg = (GradientDrawable) btnHistorySearch.getBackground();
    btnSearchBg.setColorFilter(ContextCompat.getColor(mContext, R.color.typeLeftColor), PorterDuff.Mode.SRC_ATOP);

    setDate();
    setEvent();

    return view;
  }

  private void setEvent() {

    // 환자별/작성자별 색상 변경 리스너
    typePatient.setOnClickListener(onClickListener);
    typeWriter.setOnClickListener(onClickListener);

    leftDateLayout.setOnClickListener(onClickListener);
    rightDateLayout.setOnClickListener(onClickListener);

    // 체크박스 체크 리스너
    checkAll.setOnCheckedChangeListener(onCheckedChangeListener);
    checkTemp.setOnCheckedChangeListener(onCheckedChangeListener);
    checkCert.setOnCheckedChangeListener(onCheckedChangeListener);
    checkConfirm.setOnCheckedChangeListener(onCheckedChangeListener);
    checkPrint.setOnCheckedChangeListener(onCheckedChangeListener);
  }


  private void setDate() {
    int year  = Util.toDayDate()[0];
    int month = Integer.parseInt(Util.plusZero(Util.toDayDate()[1] - 1));
    int day   = Integer.parseInt(Util.plusZero(Util.toDayDate()[2]));


    String rightDate = Util.toDayDate()[0] + "/" + Util.plusZero(Util.toDayDate()[1]) + "/" + Util.plusZero(Util.toDayDate()[2]);
    String leftDate  = Util.addDate(rightDate, "yyyy/MM/dd", 0, 0, -7, 0, 0);
    tvRightDate.setText(rightDate);
    tvLeftDate.setText(leftDate);

    leftDateDialog  = new DatePickerDialog(mContext, R.style.DatePickerDialogTheme , leftDateListener, year, month, day - 7);
    rightDateDialog = new DatePickerDialog(mContext, R.style.DatePickerDialogTheme , rightDateListener, year, month, day);
  }

  private DatePickerDialog.OnDateSetListener rightDateListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
      String m            = Util.plusZero(month + 1);
      String d            = Util.plusZero(dayOfMonth);
      String selectedDate = year + "/" + m + "/" + d;
      String ymd          = year + m + d; // 선택한 날짜
      String leftDate     = getLeftDate(); // 왼쪽 날짜
      leftDate            = Util.removeSlashStr(leftDate);

      int ymdInt      = Integer.parseInt(ymd);
      int leftDateInt = Integer.parseInt(leftDate);

      if (leftDateInt <= ymdInt) {
        setRightDate(selectedDate);
      } else {
        csAlert.show();
        csAlert.setContent("조회 마지막일 보다 지난 날짜로 지정될 수 없습니다.", ConsentConfig.ALERT_BTN_TYPE);
        csAlert.btnConfirm2.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            csAlert.dismiss();
          }
        });
      }
    }
  };

  private DatePickerDialog.OnDateSetListener leftDateListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
      String m            = Util.plusZero(month + 1);
      String d            = Util.plusZero(dayOfMonth);
      String selectedDate = year + "/" + m + "/" + d;
      String ymd          = year + m + d;
      String rightDate    = getRightDate();
      rightDate           = Util.removeSlashStr(rightDate);

      int ymdInt        = Integer.parseInt(ymd);
      int rightDateInt  = Integer.parseInt(rightDate);

      if (rightDateInt >= ymdInt) {
        setLeftDate(selectedDate);
      } else {
        csAlert.show();
        csAlert.setContent("조회 시작일 보다 이전 날짜로 지정될 수 없습니다.", ConsentConfig.ALERT_BTN_TYPE);
        csAlert.btnConfirm2.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            csAlert.dismiss();
          }
        });
      }
    }
  };



  private View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      GradientDrawable patientLeftBg = (GradientDrawable) patientLeftRound.getBackground();
      GradientDrawable patientRightBg = (GradientDrawable) patientRightRound.getBackground();
      GradientDrawable writerLeftBg = (GradientDrawable) writerLeftRound.getBackground();
      GradientDrawable writerRightBg = (GradientDrawable) writerRightRound.getBackground();

      /** 환자별 및 작성자별 색상 변경 **/
      if (v == typePatient) {
        patientLeftBg.setColorFilter(ContextCompat.getColor(mContext, R.color.typeLeftColor), PorterDuff.Mode.SRC_ATOP);
        patientRightBg.setColorFilter(ContextCompat.getColor(mContext, R.color.typeRightColor), PorterDuff.Mode.SRC_ATOP);
        ivPatient.setColorFilter(ContextCompat.getColor(mContext, R.color.white), PorterDuff.Mode.SRC_ATOP);
        tvPatient.setTextColor(ContextCompat.getColor(mContext, R.color.white));

        writerLeftBg.setColorFilter(ContextCompat.getColor(mContext, R.color.historyTypeColor), PorterDuff.Mode.SRC_ATOP);
        writerRightBg.setColorFilter(ContextCompat.getColor(mContext, R.color.departmentBtnColor), PorterDuff.Mode.SRC_ATOP);
        ivWriter.setColorFilter(ContextCompat.getColor(mContext, R.color.black), PorterDuff.Mode.SRC_ATOP);
        tvWriter.setTextColor(ContextCompat.getColor(mContext, R.color.black));

      } else if (v == typeWriter) {
        patientLeftBg.setColorFilter(ContextCompat.getColor(mContext, R.color.historyTypeColor), PorterDuff.Mode.SRC_ATOP);
        patientRightBg.setColorFilter(ContextCompat.getColor(mContext, R.color.departmentBtnColor), PorterDuff.Mode.SRC_ATOP);
        ivPatient.setColorFilter(ContextCompat.getColor(mContext, R.color.black), PorterDuff.Mode.SRC_ATOP);
        tvPatient.setTextColor(ContextCompat.getColor(mContext, R.color.black));

        writerLeftBg.setColorFilter(ContextCompat.getColor(mContext, R.color.typeLeftColor), PorterDuff.Mode.SRC_ATOP);
        writerRightBg.setColorFilter(ContextCompat.getColor(mContext, R.color.typeRightColor), PorterDuff.Mode.SRC_ATOP);
        ivWriter.setColorFilter(ContextCompat.getColor(mContext, R.color.white), PorterDuff.Mode.SRC_ATOP);
        tvWriter.setTextColor(ContextCompat.getColor(mContext, R.color.white));
        /** 환자별 및 작성자별 색상 변경 **/

        /** DatePickerDialog **/
      } else if (v == leftDateLayout) {
        leftDateDialog.show();
      }
      else if (v == rightDateLayout) {
        rightDateDialog.show();

      } /** DatePickerDialog **/
    }
  };

  private CheckBox.OnCheckedChangeListener onCheckedChangeListener = new CheckBox.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton v, boolean isChecked) {

      if (v == checkAll) {
        if (isChecked == true) {
          checkTemp.setChecked(false);
          checkCert.setChecked(false);
          checkConfirm.setChecked(false);
          checkPrint.setChecked(false);
        }
      } else if (v == checkTemp) {
        if (isChecked == true) {
          checkAll.setChecked(false);
        }
      } else if (v == checkCert) {
        if (isChecked == true) {
          checkAll.setChecked(false);
        }
      } else if (v == checkConfirm) {
        if (isChecked == true) {
          checkAll.setChecked(false);
        }
      } else if (v == checkPrint) {
        if (isChecked == true) {
          checkAll.setChecked(false);
        }
      }
      if (checkCert.isChecked() && checkTemp.isChecked() && checkConfirm.isChecked() && checkPrint.isChecked()) {
        checkAll.setChecked(true);
        checkCert.setChecked(false);
        checkTemp.setChecked(false);
        checkConfirm.setChecked(false);
        checkPrint.setChecked(false);
      }
    }
  };

  private String getLeftDate() {
    return tvLeftDate.getText().toString();
  }

  private void setLeftDate(String date) {
    tvLeftDate.setText(date);
  }

  private String getRightDate() {
    return tvRightDate.getText().toString();
  }

  private void setRightDate(String date) {
    tvRightDate.setText(date);
  }


}