package com.dbs.mplus.fatima.customView;

import android.app.Dialog;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.dbs.mplus.fatima.R;
import com.dbs.mplus.fatima.activity.ConsentActivity;
import com.dbs.mplus.fatima.httpTask.HttpCallBack;
import com.dbs.mplus.fatima.util.ConsentConfig;
import com.dbs.mplus.fatima.util.Util;

import java.util.HashMap;

public class CustomDeleteAlert extends Dialog {

  private static final String TAG = "CustomDeleteAlert";
  private Context mContext;
  private HttpCallBack callBack;

  private RadioButton tempRadio1;
  private RadioButton tempRadio2;
  private RadioButton tempRadio3;
  private RadioButton tempRadio4;
  private EditText tempEditText;
  private Button btnConfirm;
  private Button btnCancel;

  private String userId = "";
  private HashMap<String, String> mData;

  public CustomDeleteAlert(@NonNull Context context, HashMap<String, String> mData, HttpCallBack callBack) {
    super(context);
    this.mContext = context;
    this.mData    = mData;
    this.callBack = callBack;
  }

  public CustomDeleteAlert(@NonNull Context context, int themeResId) {
    super(context, themeResId);
  }

  protected CustomDeleteAlert(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
    super(context, cancelable, cancelListener);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    setCanceledOnTouchOutside(false);
    setContentView(R.layout.custom_delete_alert);

    init();
  }

  private void init() {
    userId = Util.getStringPreference(mContext, "userInfo", "userId");
    setLayout();
    setEvent();
  }

  private void setLayout() {
    tempRadio1   = (RadioButton) findViewById(R.id.tempRadio1);
    tempRadio2   = (RadioButton) findViewById(R.id.tempRadio2);
    tempRadio3   = (RadioButton) findViewById(R.id.tempRadio3);
    tempRadio4   = (RadioButton) findViewById(R.id.tempRadio4);
    tempEditText = (EditText) findViewById(R.id.tempEditText);
    btnConfirm   = (Button) findViewById(R.id.btnConfirm);
    btnCancel    = (Button) findViewById(R.id.btnCancel);

    tempRadio1.setChecked(true);
    tempEditText.setEnabled(false);

    Drawable confirmBg  = (Drawable) btnConfirm.getBackground();
    Drawable cancelBg   = (Drawable) btnCancel.getBackground();

    confirmBg.setColorFilter(ContextCompat.getColor(mContext, R.color.alertConfirmColor), PorterDuff.Mode.SRC_ATOP);
    cancelBg.setColorFilter(ContextCompat.getColor(mContext, R.color.alertCancelColor), PorterDuff.Mode.SRC_ATOP);
  }

  private void setEvent() {
    btnCancel.setOnClickListener(onClickListener);
    btnConfirm.setOnClickListener(onClickListener);

    tempRadio1.setOnCheckedChangeListener(onCheckedChangeListener);
    tempRadio2.setOnCheckedChangeListener(onCheckedChangeListener);
    tempRadio3.setOnCheckedChangeListener(onCheckedChangeListener);
    tempRadio4.setOnCheckedChangeListener(onCheckedChangeListener);
  }

  private View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      if (btnCancel == v) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(tempEditText.getWindowToken(), 0);
        dismiss();
      } else if (btnConfirm == v) {
        deleteProcess();
      }
    }
  };

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

      if (buttonView == tempRadio1 && isChecked == true) {
        tempEditText.setEnabled(false);
      } else if (buttonView == tempRadio2 && isChecked == true) {
        tempEditText.setEnabled(false);
      } else if (buttonView == tempRadio3 && isChecked == true) {
        tempEditText.setEnabled(false);
      } else if (buttonView == tempRadio4 && isChecked == true) {
        tempEditText.setEnabled(true);
      }

    }
  };

  private void deleteProcess() {

    String reason = "";
    if (tempRadio1.isChecked() == true) {
      reason = tempRadio1.getText().toString().trim();
    } else if (tempRadio2.isChecked() == true) {
      reason = tempRadio2.getText().toString().trim();
    } else if (tempRadio3.isChecked() == true) {
      reason = tempRadio3.getText().toString().trim();
    } else if (tempRadio4.isChecked() == true) {
      reason = tempEditText.getText().toString().trim();
    }

    if (reason.equals("")) {
      Util.getOKDialog(mContext, mContext.getString(R.string.alrim), mContext.getString(R.string.deleteReasonStr), null).show();
      return;
    }

    ((ConsentActivity) mContext).showProgress();

    mData.put("reasonForUseN", reason);

    Log.e(TAG, "mData -- > " + mData);

    Util.callHttp(mContext, ConsentConfig.HOST_CONSENT, ConsentConfig.SAVE_DELETE, mData, callBack);

  }

  @Override
  public void show() {
    super.show();
  }

  @Override
  public void dismiss() {
    super.dismiss();
  }
}
