package com.lemonhc.mplus.mcare_plus.activity.Util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.lemonhc.mplus.mcare_plus.R;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import androidx.core.content.ContextCompat;

public class Util {

  public static AlertDialog getPasswordInputDialog(Context context, String title, String msg, DialogInterface.OnClickListener okListener, DialogInterface.OnClickListener cancelListener) {

    String okId = context.getString(R.string.confirm);
    String cancelId = context.getString(R.string.cancel);

    AlertDialog dlg = createDialog(context, title, msg, okId, okListener, cancelId, cancelListener);
    dlg.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    return dlg;
  }
  private static AlertDialog createDialog(Context context, String title, String msg, String button1, DialogInterface.OnClickListener button1Listener, String button2, DialogInterface.OnClickListener button2Listener) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(context, android.R.style.Theme_Holo_Light_Dialog));

    builder.setTitle(title);
    builder.setMessage(msg);
    builder.setPositiveButton(button1, button1Listener);
    if (button2Listener != null) {
      builder.setNegativeButton(button2, button2Listener);
    }
    builder.setCancelable(false);

    return builder.create();
  }

  public static void setCursorColor(EditText view, int color) {
    try {
      // Get the cursor resource id
      Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
      field.setAccessible(true);
      int drawableResId = field.getInt(view);

      // Get the editor
      field = TextView.class.getDeclaredField("mEditor");
      field.setAccessible(true);
      Object editor = field.get(view);

      // Get the drawable and set a color filter
      Drawable drawable = ContextCompat.getDrawable(view.getContext(), drawableResId);
      drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
      Drawable[] drawables = {drawable, drawable};

      // EditTextView underLine color
      Drawable drawable1 = view.getBackground();
      drawable1.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
      view.setBackground(drawable1);

      // Set the drawables
      field = editor.getClass().getDeclaredField("mCursorDrawable");
      field.setAccessible(true);
      field.set(editor, drawables);
    } catch (Exception ignored) {
    }
  }

  public static AlertDialog getOKDialog(Context context, String title, String msg, DialogInterface.OnClickListener okListener) {
    String okId = context.getString(R.string.confirm);
    AlertDialog dlg = createDialog(context, title, msg, okId, okListener, "", null);
    dlg.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    return dlg;
  }

  public static void LOG(Class<?> clazz, String msg) {

    printDebug(clazz, msg, "error");
    //Log.d(MPlusFrameworkConstants.LOG_TAG, buildMessage(clazz, msg));

  }

  private static void printDebug(Class<?> clazz, String msg, String logType) {
    byte[] msgByte = msg.getBytes();
    int startIndex = 0;
    int bufferSize = 3500;
    while(msgByte.length > bufferSize) {
      if (logType.equals("error")) {
        Log.e("JKSONG", buildMessage(clazz, new String(msgByte, startIndex, bufferSize-startIndex)));
      } else {
        Log.d("JKSONG", buildMessage(clazz, new String(msgByte, startIndex, bufferSize-startIndex)));
      }

      startIndex = bufferSize;
      bufferSize = bufferSize + 3500;
    }

    if (logType.equals("error")) {
      Log.e("JKSONG", buildMessage(clazz, new String(msgByte, startIndex, msgByte.length-startIndex)));

    } else {
      Log.d("JKSONG", buildMessage(clazz, new String(msgByte, startIndex, msgByte.length-startIndex)));
    }

  }

  private static String buildMessage(Class<?> clazz, String msg) {
    final StringBuilder builder = new StringBuilder();

    // 시간
    builder.append(format.format(new Date())).append(" ");
    // 클래스명
    builder.append("[").append(clazz.getSimpleName()).append("] ");
    if(msg != null) {
      builder.append(msg);
    }

    return builder.toString();
  }
  private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault());
}
