package com.dbs.dbsec.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.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;

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

import com.dbs.dbsec.BuildConfig;
import com.dbs.dbsec.R;
import com.dbs.dbsec.activity.ConsentActivity;
import com.dbs.dbsec.util.Common;

import java.util.HashMap;

public class CustomSettingAlertDialog extends Dialog {
  
  private static final String TAG = "CustomSettingAlertDialog";
  private Context mContext;
  
  private RadioButton inPatientRid;
  private RadioButton outPatientRid;
  private RadioButton erPatientRid;
  private RadioButton opPatientRid;
  private RadioButton searchRid;
  private Button btnConfirm;
  private Button btnCancel;
  
  private String userId    = "";
  private String indexPage = "";
  private String status    = "";
  
  public CustomSettingAlertDialog(@NonNull Context context) {
    super(context);
    this.mContext = context;
  }
  
  public CustomSettingAlertDialog(@NonNull Context context, int themeResId) {
    super(context, themeResId);
  }
  
  protected CustomSettingAlertDialog(@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_setting_alert);
    
    init();
  }
  
  private void init() {
    userId = Common.getStringPreference(mContext, "userInfo", "userId");
    indexPage = Common.getStringPreference(mContext,  "userInfo", "indexPage");
    
    setLayout();
    setSetting();
    setEvent();
  }
  
  private void setLayout() {
    inPatientRid  = (RadioButton) findViewById(R.id.inPatientRid);
    outPatientRid = (RadioButton) findViewById(R.id.outPatientRid);
    erPatientRid  = (RadioButton) findViewById(R.id.erPatientRid);
    opPatientRid  = (RadioButton) findViewById(R.id.opPatientRid);
    searchRid     = (RadioButton) findViewById(R.id.searchRid);
    btnConfirm    = (Button) findViewById(R.id.btnConfirm);
    btnCancel     = (Button) findViewById(R.id.btnCancel);

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

    Drawable confirmBg  = (Drawable) btnConfirm.getBackground();
    Drawable cancelBg   = (Drawable) btnCancel.getBackground();
    confirmBg.setColorFilter(ContextCompat.getColor(mContext, R.color.commonColor), PorterDuff.Mode.SRC_ATOP);

    cancelBg.setColorFilter(ContextCompat.getColor(mContext, R.color.alertCancelColor), PorterDuff.Mode.SRC_ATOP);
  }
  
  private void setSetting() {
    status = indexPage;
    if (indexPage.equals("I")) { // 입원
      inPatientRid.setChecked(true);
    } else if (indexPage.equals("O")) { // 외래
      outPatientRid.setChecked(true);
    } else if (indexPage.equals("E")) { // 응급
      erPatientRid.setChecked(true);
    } else if (indexPage.equals("OP")) { // 수술
      opPatientRid.setChecked(true);
    } else if (indexPage.equals("SR")) { // 검색
      searchRid.setChecked(true);
    }
  }
  
  private void setEvent() {
    btnConfirm.setOnClickListener(onClickListener);
    btnCancel.setOnClickListener(onClickListener);
    
    inPatientRid.setOnCheckedChangeListener(onCheckedChangeListener);
    outPatientRid.setOnCheckedChangeListener(onCheckedChangeListener);
    erPatientRid.setOnCheckedChangeListener(onCheckedChangeListener);
    opPatientRid.setOnCheckedChangeListener(onCheckedChangeListener);
    searchRid.setOnCheckedChangeListener(onCheckedChangeListener);
  }
  
//  private void settingSubmit() {
//    ((ConsentActivity) mContext).showProgress();
//    final CallBack callBack = new CallBack() {
//      @Override
//      public void result(SoapObject result) {
//        if (result == null || result.toString().indexOf("null") > 0 || result.getPropertyCount() == 0) {
//          stop();
//        } else {
//          String rts = result.getProperty("responseData").toString().trim();
//          int res = Integer.parseInt(rts);
//
//          if (res == 0) {
//            stop();
//          } else {
//            Util.setStringPreference(mContext, "userInfo", "indexPage", status);
//          }
//
//          ((ConsentActivity) mContext).dismissProgress();
//          dismiss();
//        }
//      }
//
//      @Override
//      public void stop() {
//        ((ConsentActivity) mContext).showSingButtonDialog(mContext.getString(R.string.settingError));
////        ((ConsentActivity) mContext).dismissProgress();
////        dismiss();
//      }
//
//      @Override
//      public void error() {
//
//      }
//    };
//    String str = "";
//    String message = mContext.getString(R.string.settingAlertMsg);
//    if (status.equals("I")) {
//      str = mContext.getString(R.string.admission);
//    } else if (status.equals("O")) {
//      str = mContext.getString(R.string.outPatient);
//    } else if (status.equals("E")) {
//      str = mContext.getString(R.string.emergency);
//    } else if (status.equals("OP")) {
//      str = mContext.getString(R.string.surgery);
//    } else {
//      str = mContext.getString(R.string.search);
//    }
//
//    message = String.format(message, str);
//    ((ConsentActivity) mContext).csAlert.show();
//    ((ConsentActivity) mContext).csAlert.setContent(message, "");
//
//    ((ConsentActivity) mContext).csAlert.btnConfirm.setOnClickListener(new View.OnClickListener() {
//      @Override
//      public void onClick(View v) {
//        HashMap<String, String> mData = new HashMap<String, String>();
//        mData.put("userId", userId);
//        mData.put("status", status);
//        mData.put("instCd", BuildConfig.INST_CD);
//        Common.callHttp(mContext, ConsentConfig.HOST_HOSPITAL, ConsentConfig.UPDATE_USER_SETUP , mData, callBack);
//        ((ConsentActivity) mContext).csAlert.dismiss();
//      }
//    });
//
//    ((ConsentActivity) mContext).csAlert.btnCancel.setOnClickListener(new View.OnClickListener() {
//      @Override
//      public void onClick(View v) {
//        ((ConsentActivity) mContext).csAlert.dismiss();
//        ((ConsentActivity) mContext).dismissProgress();
//        dismiss();
//      }
//    });
//  }
  
  private RadioButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      if (inPatientRid == buttonView && isChecked) {
        status = "I";
      } else if (outPatientRid == buttonView && isChecked) {
        status = "O";
      } else if (erPatientRid == buttonView && isChecked) {
        status = "E";
      } else if (opPatientRid == buttonView && isChecked) {
        status = "OP";
      } else if (searchRid == buttonView && isChecked) {
        status = "SR";
      }
    }
  };
  
  private View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      if (btnConfirm == v) {
//        settingSubmit();
      } else if (btnCancel == v) {
        dismiss();
      }
    }
  };
  
  @Override
  public void show() {
    super.show();
  }
  
  @Override
  public void dismiss() {
    super.dismiss();
  }
}
