package com.ntels.onecable.admin.common.handler;

import com.ntels.onecable.admin.common.Constants;
import com.ntels.onecable.admin.common.util.Dlog;
import com.ntels.onecable.admin.common.util.Util;

import android.app.Activity;
import android.os.Handler;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by jinochoi on 2017. 3. 31..
 */

public class CallWithDelayHandler {

    private long mBackKeyPressedTime = 0;
    private Activity mActivity;
    private Toast mToast;
    private Util mUtil = new Util();

    public CallWithDelayHandler(Activity activity) {
        this.mActivity = activity;
    }



    public void callFinishWithDelay(String isMethodName) {

        callWithDelayMethod(isMethodName, true, "finish", Constants.HANDLER_DELAY_MILLIS);

    }

    public void callFinishWithDelay(String isMethodName, Boolean equalValue) {

        callWithDelayMethod(isMethodName, equalValue, "finish", Constants.HANDLER_DELAY_MILLIS);

    }


    public void callWithDelay(final String callMethodName) {

        callWithDelayMethod("", true, callMethodName, Constants.HANDLER_DELAY_MILLIS);

    }

    public void callWithDelay(final String callMethodName, int delayTime) {

        callWithDelayMethod("", true, callMethodName, delayTime);

    }

    public void callWithDelay(String isMethodName, String callMethodName) {

        callWithDelayMethod(isMethodName, true, callMethodName, Constants.HANDLER_DELAY_MILLIS);
    }

    public void callWithDelay(String isMethodName, Boolean equalValue, String callMethodName) {

        callWithDelayMethod(isMethodName, equalValue, callMethodName, Constants.HANDLER_DELAY_MILLIS);
    }

    public void callWithDelayMethod(final String isMethodName, final Boolean equalValue, final String callMethodName, final int delayTime) {
        final Handler handler = new Handler();
        final Runnable runnable = new Runnable() {
            public void run() {
                try {
                    Boolean isProc = false;

                    Dlog.v("isMethodName = " + isMethodName);
                    if (!isMethodName.equals("")) {
                        Method isMethod;
                        try {
                            isMethod = mActivity.getClass().getMethod(isMethodName);
                        } catch (NoSuchMethodException e) {
                            isMethod = mActivity.getClass().getDeclaredMethod(isMethodName);
                            isMethod.setAccessible(true);
                        }

                        Object returnValue = isMethod.invoke(mActivity);
                        Dlog.v("returnValue = " + returnValue);
                        if (returnValue == equalValue) {
                            isProc = true;
                        }
                    } else {
                        isProc = true;
                    }
                    Dlog.v("callMethodName = " + callMethodName);
                    if (isProc) {
                        if (callMethodName.equals("finish")) {
                            mActivity.finish();
                        } else {
                            Method method;
                            try {
                                method = mActivity.getClass().getMethod(callMethodName);
                            } catch (NoSuchMethodException e) {
                                method = mActivity.getClass().getDeclaredMethod(callMethodName);
                                method.setAccessible(true);
                            }
                            method.invoke(mActivity);
                        }
                    } else {
                        if (!mActivity.isFinishing()) {
                            callWithDelayMethod(isMethodName, equalValue, callMethodName, delayTime);
                        }
                    }
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        };
        handler.postDelayed(runnable, delayTime);
    }

    public void callFinishWithDelayField(String isFieldName) {

        callWithDelayField(isFieldName, true, "finish", Constants.HANDLER_DELAY_MILLIS);

    }

    public void callFinishWithDelayField(String isFieldName, Boolean equalValue) {

        callWithDelayField(isFieldName, equalValue, "finish", Constants.HANDLER_DELAY_MILLIS);

    }

    public void callWithDelayField(final String callMethodName) {

        callWithDelayField("", true, callMethodName, Constants.HANDLER_DELAY_MILLIS);

    }

    public void callWithDelayField(final String callMethodName, int delayTime) {

        callWithDelayField("", true, callMethodName, delayTime);

    }

    public void callWithDelayField(String isFieldName, String callMethodName) {

        callWithDelayField(isFieldName, true, callMethodName, Constants.HANDLER_DELAY_MILLIS);
    }

    public void callWithDelayField(String isFieldName, String callMethodName, int delayTime) {

        callWithDelayField(isFieldName, true, callMethodName, delayTime);
    }

    public void callWithDelayField(String isFieldName, Boolean equalValue, String callMethodName) {

        callWithDelayField(isFieldName, equalValue, callMethodName, Constants.HANDLER_DELAY_MILLIS);
    }

    public void callWithDelayField(final String isFieldName, final Boolean equalValue, final String callMethodName, final int delayTime) {
        final Handler handler = new Handler();
        final Runnable runnable = new Runnable() {
            public void run() {
                try {
                    Boolean isProc = false;

                    Field isField;
                    try {
                        isField = mActivity.getClass().getField(isFieldName);
                    } catch (NoSuchFieldException e) {
                        isField = mActivity.getClass().getDeclaredField(isFieldName);
                        isField.setAccessible(true);
                    }

                    Object returnValue = isField.get(mActivity);
                    Dlog.v("returnValue = " + returnValue);
                    if (returnValue == equalValue) {
                        isProc = true;
                    }
                    if (isProc) {
                        if (callMethodName.equals("finish")) {
                            mActivity.finish();
                        } else {
                            Method method;
                            try {
                                method = mActivity.getClass().getMethod(callMethodName);
                            } catch (NoSuchMethodException e) {
                                method = mActivity.getClass().getDeclaredMethod(callMethodName);
                                method.setAccessible(true);
                            }
                            method.invoke(mActivity);
                        }
                    } else {
                        if (!mActivity.isFinishing()) {
                            callWithDelayField(isFieldName, equalValue, callMethodName, delayTime);
                        }
                    }
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        };
        handler.postDelayed(runnable, delayTime);
    }

    private boolean doesReturnVoid(Method method) {
        Boolean isReturn = false;
        if (void.class.equals(method.getReturnType())) {
            isReturn = true;
        }

        return isReturn;
    }

}
