package com.dbs.mplus.fatima.customView;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

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

import com.dbs.mplus.fatima.R;
import com.dbs.mplus.fatima.util.ConsentConfig;

public class CustomAlertDialog extends Dialog {

  private static final String TAG = "CustomAlertDialog";
  private Context mContext;
  public LinearLayout btnLayout;
  public Button btnConfirm;
  public Button btnConfirm2;
  public Button btnCancel;
  public TextView tvAlertMsg;

  public String message = "";

  public CustomAlertDialog(@NonNull Context context) {
    super(context);
    mContext = context;
  }

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

    init();
  }

  private void init() {
    setLayout();
  }

  private void setLayout() {
    btnLayout   = (LinearLayout) findViewById(R.id.btnLayout);
    btnConfirm  = (Button) findViewById(R.id.btnConfirm);
    btnConfirm2 = (Button) findViewById(R.id.btnConfirm2);
    btnCancel   = (Button) findViewById(R.id.btnCancel);
    tvAlertMsg  = (TextView) findViewById(R.id.tvAlertMsg);

    setCanceledOnTouchOutside(false); //화면 밖을 터치했을때 사라지지 않게

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

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

  public void setContent(String message, String btnType) {
    if (btnType.equals(ConsentConfig.ALERT_BTN_TYPE)) {
      btnLayout.setVisibility(View.GONE);
      btnConfirm2.setVisibility(View.VISIBLE);
    } else {
      btnLayout.setVisibility(View.VISIBLE);
      btnConfirm2.setVisibility(View.GONE);
    }

    tvAlertMsg.setText(message);
  }

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

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