package com.dbs.consent.demo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.dbs.consent.demo.R;
import com.dbs.consent.demo.activity.ConsentActivity;
import com.dbs.consent.demo.model.PatientListVO;

import java.util.ArrayList;
import java.util.HashMap;

public class PatientListAdapter extends RecyclerView.Adapter<PatientListAdapter.ViewHolder> {

  private static final String TAG = "PatientListAdapter";
  private AppCompatActivity mActivity;
  private Context mContext;
  private ConsentActivity instance;
  public ArrayList<PatientListVO> arrayList;
  private String indexPage = "";
  private int rowIndex = 0;
  public boolean click = false;

  public PatientListAdapter(AppCompatActivity mActivity, Context mContext, ArrayList<PatientListVO> patientLists, String indexPage) {
    this.mActivity = mActivity;
    this.mContext  = mContext;
    this.instance  = (ConsentActivity) mContext;
    this.arrayList = patientLists;
    this.indexPage = indexPage;
  }

  @Override
  public int getItemCount() {
    return arrayList.size();
  }

  public class ViewHolder extends RecyclerView.ViewHolder {
    public int position = 0;
    public LinearLayout listLayer;
    public TextView tvPid;      //등록번호
    public TextView tvFsex;     //성별/나이
    public TextView tvDept;     //진료과
    public TextView tvPatient;  //환자명
    public TextView tvPract;    //담당의
    public TextView tvWard;     //병실

    public ViewHolder(@NonNull View itemView) {
      super(itemView);
    }
  }

  @NonNull
  @Override
  public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.patient_list_item, parent, false);
    ViewHolder holder = new ViewHolder(view);

    holder.listLayer = (LinearLayout) view.findViewById(R.id.listLayer);
    holder.tvPid     = (TextView) view.findViewById(R.id.tvPid);
    holder.tvFsex    = (TextView) view.findViewById(R.id.tvFsex);
    holder.tvDept    = (TextView) view.findViewById(R.id.tvDept);
    holder.tvPatient = (TextView) view.findViewById(R.id.tvPatient);
    holder.tvPract   = (TextView) view.findViewById(R.id.tvPract);
    holder.tvWard    = (TextView) view.findViewById(R.id.tvWard);

    holder.listLayer.setTag(holder);
    return holder;
  }

  @Override
  public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.position = position;
    PatientListVO patientListVO = arrayList.get(position);
    String pid       = patientListVO.getPid();
    String sa        = patientListVO.getSa();
    String deptNm    = patientListVO.getDeptNm();
    String patientNm = patientListVO.getHngNm();
    String doctorNm  = patientListVO.getDoctorNm();
    String roomCd    = patientListVO.getRoomCd();
    String opRoom    = patientListVO.getOpRoomNm();
    String inDd      = patientListVO.getInDd();

    holder.tvPid.setText(pid);
    holder.tvFsex.setText(sa);
    holder.tvDept.setText(deptNm);
    holder.tvPatient.setText(patientNm);
    holder.tvPract.setText(doctorNm);
    if (instance.indexPage.equals("I")) {
      holder.tvWard.setText(roomCd);
    } else if (instance.indexPage.equals("OP")) {
      holder.tvWard.setText(opRoom);
    } else if (instance.indexPage.equals("SR")) {
      holder.tvWard.setText(inDd);
    }



    if (rowIndex == holder.position && click == true) {
      holder.listLayer.setBackgroundColor(mActivity.getColor(R.color.commonColor));
      holder.tvPid.setTextColor(mActivity.getColor(R.color.white));
      holder.tvFsex.setTextColor(mActivity.getColor(R.color.white));
      holder.tvDept.setTextColor(mActivity.getColor(R.color.white));
      holder.tvPatient.setTextColor(mActivity.getColor(R.color.white));
      holder.tvPract.setTextColor(mActivity.getColor(R.color.white));
      holder.tvWard.setTextColor(mActivity.getColor(R.color.white));
    } else {
      holder.listLayer.setBackgroundColor(mActivity.getColor(R.color.white));
      holder.tvPid.setTextColor(mActivity.getColor(R.color.userDeptColor));
      holder.tvFsex.setTextColor(mActivity.getColor(R.color.sexNAgeColor));
      holder.tvDept.setTextColor(mActivity.getColor(R.color.darkGray));
      holder.tvPatient.setTextColor(mActivity.getColor(R.color.black));
      holder.tvPract.setTextColor(mActivity.getColor(R.color.darkGray));
      holder.tvWard.setTextColor(mActivity.getColor(R.color.darkGray));
    }

    holder.listLayer.setOnClickListener(onClickListener);
  }

  private View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      ViewHolder holder = (ViewHolder) v.getTag();
      int position = holder.getLayoutPosition();
      PatientListVO patientListVO = arrayList.get(position);
      String pid       = patientListVO.getPid();
      String inDd      = patientListVO.getInDd();
      String ordType   = patientListVO.getOrdType();
      String ordDeptCd = patientListVO.getOrdDeptCd();
      String cretNo    = patientListVO.getCretNo();
      String instCd    = patientListVO.getInstCd();
      String opRsrvNo  = patientListVO.getOprsRvNo ();

      Log.e(TAG, "pid       -- > " + pid      );
      Log.e(TAG, "inDd      -- > " + inDd     );
      Log.e(TAG, "ordType   -- > " + ordType  );
      Log.e(TAG, "ordDeptCd -- > " + ordDeptCd);
      Log.e(TAG, "cretNo    -- > " + cretNo   );
      Log.e(TAG, "instCd    -- > " + instCd   );
      Log.e(TAG, "opRsrvNo  -- > " + opRsrvNo );

      if (v == holder.listLayer) {
        rowIndex = position;
        click = true;
        notifyDataSetChanged();
        HashMap<String, String> mData = new HashMap<String, String>();
        mData.put("pid", pid);
        mData.put("inDd", inDd);
        mData.put("ordType", ordType);
        mData.put("ordDeptCd", ordDeptCd);
        mData.put("cretNo", cretNo);
        mData.put("instCd", instCd);
        mData.put("opRsrvNo", opRsrvNo);
        instance.centerMenu.getPatientInfo(mData);
        instance.centerMenu.getConsentList(pid, inDd);
      }
    }
  };
}
