package com.dbs.dbsec.activity.adapter;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dbs.dbsec.R;
import com.dbs.dbsec.activity.ConsentActivity;
import com.dbs.dbsec.consent.sign.SignWrapper;
import com.dbs.dbsec.model.UserLoginDeptList;
import com.dbs.dbsec.util.Common;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

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

  private static final String TAG = "DepartmentAdapter";
  private Activity mActivity;
  public ArrayList<UserLoginDeptList> arrayList;

  private int lastSelectedPosition = 0;

  public DepartmentAdapter(Activity mActivity, ArrayList<UserLoginDeptList> arrayList) {
    this.mActivity = mActivity;
    this.arrayList = arrayList;

    Log.e(TAG, "arrayList size111 -- > " + arrayList.size());
  }

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

  public class ViewHolder extends RecyclerView.ViewHolder {
    RelativeLayout itemLayout;
    RadioButton radioBtn;
    TextView tvItem;
    Button btnSave;
    Button btnCancel;
    int position = 0;
    public ViewHolder(@NonNull View itemView) {
      super(itemView);
      itemLayout = (RelativeLayout) itemView.findViewById(R.id.itemLayout);
      radioBtn = (RadioButton) itemView.findViewById(R.id.radioBtn);

      radioBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          lastSelectedPosition = getAdapterPosition();
          notifyDataSetChanged();
        }
      });

      itemLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          lastSelectedPosition = getAdapterPosition();
          radioBtn.setChecked(true);
          notifyDataSetChanged();
        }
      });
    }
  }

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

    holder.itemLayout = (RelativeLayout) view.findViewById(R.id.itemLayout);
    holder.radioBtn   = (RadioButton) view.findViewById(R.id.radioBtn);
    holder.tvItem     = (TextView) view.findViewById(R.id.tvItem);
    holder.btnSave    = (Button) mActivity.findViewById(R.id.btnSave);
    holder.btnCancel  = (Button) mActivity.findViewById(R.id.btnCancel);

    holder.itemLayout.setTag(holder);
    holder.radioBtn.setTag(holder);
    holder.tvItem.setTag(holder);
    holder.btnSave.setTag(holder);
    holder.btnCancel.setTag(holder);
    return holder;
  }

  @Override
  public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    UserLoginDeptList deptList = arrayList.get(position);
    String depthngnm = deptList.getDeptHngNm();

    holder.radioBtn.setChecked(lastSelectedPosition == position);
    holder.position = position;
    holder.tvItem.setText(depthngnm);

    holder.btnSave.setOnClickListener(onClickListener);
    holder.btnCancel.setOnClickListener(onClickListener);
  }

  private View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      ViewHolder holder = (ViewHolder) v.getTag();
      if (v == holder.btnCancel) {
        try {
          SignWrapper.getInstance().deleteAll();
          Common.clearPreference(mActivity, "userInfo");
          Common.clearPreference(mActivity, "sessionTime");
        } catch (Exception e) {
        }
        mActivity.finish();
      } else {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
        Date nowDate = new Date();
        String nowDateStr = dateFormat.format(nowDate);

        Common.setStringPreference(mActivity, "sessionTime", "sessionTime", nowDateStr);

        UserLoginDeptList userLoginDeptList = arrayList.get(lastSelectedPosition);
        String deptCd    = userLoginDeptList.getDutplceCd();
        String deptNm    = userLoginDeptList.getDeptHngNm();
        String jobKindCd = userLoginDeptList.getJobKindCd();
        String jobKindNm = userLoginDeptList.getJobKindNm();
        String userId    = userLoginDeptList.getUserId();
        String userName  = userLoginDeptList.getUserName();
        String indexPage = userLoginDeptList.getIndexPage();
        String drKind    = userLoginDeptList.getDrKind();

        Common.setStringPreference(mActivity, "userInfo", "deptCd", deptCd);
        Common.setStringPreference(mActivity, "userInfo", "deptNm", deptNm);
        Common.setStringPreference(mActivity, "userInfo", "jobKindCd", jobKindCd);
        Common.setStringPreference(mActivity, "userInfo", "jobKindNm", jobKindNm);
        Common.setStringPreference(mActivity, "userInfo", "userId", userId);
        Common.setStringPreference(mActivity, "userInfo", "userName", userName);
        Common.setStringPreference(mActivity, "userInfo", "indexPage", indexPage);
        Common.setStringPreference(mActivity, "userInfo", "drKind", drKind);

        Intent intent = new Intent(mActivity, ConsentActivity.class);
        intent.putExtra("deptCd", deptCd);
        intent.putExtra("deptNm", deptNm);
        intent.putExtra("jobKindCd", jobKindCd);
        intent.putExtra("jobKindNm", jobKindNm);
        intent.putExtra("userId", userId);
        intent.putExtra("userName", userName);
        intent.putExtra("indexPage", indexPage);
        intent.putExtra("drKind", drKind);
        mActivity.setResult(Activity.RESULT_OK);
        mActivity.startActivityForResult(intent, 0);
        mActivity.finish();
      }
    }
  };
}
