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.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.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.dbs.mplus.knuh.R;


public class HistoryFragment extends Fragment {

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

  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;

  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);

    typePatient       = (LinearLayout) view.findViewById(R.id.typePatient);
    Log.e(TAG, "typePatient --> " + 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);

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

    return view;
  }

  public void setEvent() {

    typePatient.setOnClickListener(onClickListener);
    typeWriter.setOnClickListener(onClickListener);

  }


  public 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) {
        Log.e(TAG,"v -- > " + v);
        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) {
        Log.e(TAG,"v -- > " + v);
        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));
      }
    }
  };
}
