package kr.co.hanwha.hifive.util;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.firebase.analytics.FirebaseAnalytics;

import kr.co.hanwha.hifive.BuildConfig;
import kr.co.hanwha.hifive.base.FirebaseAnalyticsInterface;

/**
 * Created by sanghoonkwon on 2018. 2. 14..
 */

public class HFAnalyticsUtil {

    private static final String TAG = HFAnalyticsUtil.class.getSimpleName();

    private static final String KEY_HF_EVENT = "HF_EVENT";
    private static final String KEY_HF_TEST_EVENT = "HF_TEST_EVENT";
    private static final String KEY_HF_OS_VERSION = "HF_OS_VERSION";
    private static final String KEY_HF_APP_VERSION = "HF_APP_VERSION";
    private static final String KEY_HF_DEVICE_NAME = "HF_DEVICE_NAME";
    private static final String KEY_HF_URI = "HF_URI";
    private static final String KEY_HF_CLASS_NAME = "HF_CLASS_NAME";

    private static final String BASE_URI = "quest.hanwha://mobileapp/v1";

    public static final String HF_EVENT = "/event";

    public static final String HF_MY_OKRS = "/myokrs";
    public static final String HF_TASK_ID = "/task/%s";
    public static final String HF_TASK_REPLY = HF_TASK_ID + "/reply";
    public static final String HF_TASK_HISTORY = HF_TASK_ID + "/history";
    public static final String HF_TASK_EDIT = HF_TASK_ID + "/edit";

    public static final String HF_PROJECT = "/project/%s";
    public static final String HF_PROJECT_RUNNING = HF_PROJECT + "/running";
    public static final String HF_PROJECT_ALL = HF_PROJECT + "/all";

    public static final String HF_NOTIFICATIONS = "/notifications";

    public static final String HF_SEARCH = "/search";
    public static final String HF_SEARCH_QUERY = HF_SEARCH + "?q=%s";

    public static final String HF_LOGIN = "/login";
    public static final String HF_LOGIN_FAIL_WRONGPASSWORD = HF_LOGIN + "/fail/wrongpassword";
    public static final String HF_LOGIN_FAIL_WRONGINPUT = HF_LOGIN + "/fail/wronginput";

    public static final String HF_QAUTH = "/qauth";
    public static final String HF_QAUTH_FAIL = HF_QAUTH + "fail";

    public static final String HF_GNB = "/gnb";

    public static final String HF_GNB_TASKBOX = HF_GNB + "/taskbox";

    public static final String HF_GNB_TASKBOX_TASKS = HF_GNB_TASKBOX + "/tasks";
    public static final String HF_GNB_TASKBOX_TASKS_TODAY = HF_GNB_TASKBOX_TASKS + "/today";
    public static final String HF_GNB_TASKBOX_TASKS_FINISHED = HF_GNB_TASKBOX_TASKS + "/finished";
    public static final String HF_GNB_TASKBOX_TASKS_TASK_ID = HF_GNB_TASKBOX_TASKS + "/%s";

    public static final String HF_GNB_TASKBOX_REQUESTS = HF_GNB_TASKBOX + "/requests";
    public static final String HF_GNB_TASKBOX_REQUESTS_TOP = HF_GNB_TASKBOX_REQUESTS + "/top";
    public static final String HF_GNB_TASKBOX_REQUESTS_FINISHED = HF_GNB_TASKBOX_REQUESTS + "/finished";
    public static final String HF_GNB_TASKBOX_REQUESTS_TASK_ID = HF_GNB_TASKBOX_REQUESTS + "/%s";

    public static final String HF_GNB_MYPROJECTS = HF_GNB + "/myprojects";
    public static final String HF_GNB_MYPROJECTS_PROJECT_ID = HF_GNB_MYPROJECTS + "/%s";

    public static final String HF_GNB_NEWTASK = HF_GNB + "/newtask";
    public static final String HF_GNB_NEWTASK_DUEDATE = HF_GNB_NEWTASK + "/duedate";
    public static final String HF_GNB_NEWTASK_KRS = HF_GNB_NEWTASK + "/krs";
    public static final String HF_GNB_NEWTASK_RECEIVER = HF_GNB_NEWTASK + "/receiver";

    public static final String HF_GNB_DASHBOARD = HF_GNB + "/dashboard";
    public static final String HF_GNB_DASHBOARD_REDYELLOW = HF_GNB_DASHBOARD + "/redyellow";
    public static final String HF_GNB_DASHBOARD_REDYELLOW_ID = HF_GNB_DASHBOARD_REDYELLOW + "/%s";
    public static final String HF_GNB_DASHBOARD_GREEN = HF_GNB_DASHBOARD + "/green";
    public static final String HF_GNB_DASHBOARD_GREEN_ID = HF_GNB_DASHBOARD_GREEN + "/%s";

    public static final String HF_GNB_CONFIG = HF_GNB + "/config";
    public static final String HF_GNB_CONFIG_CHECKPASSWORD = HF_GNB_CONFIG + "/checkpassword";

    public static final String HF_GNB_CONFIG_MYINFO = HF_GNB_CONFIG + "/myinfo";
    public static final String HF_GNB_CONFIG_MYINFO_CHANGEPASSWORD = HF_GNB_CONFIG_MYINFO + "/changepassword";
    public static final String HF_GNB_CONFIG_MYINFO_CHANGEQAUTH = HF_GNB_CONFIG_MYINFO + "/changeqauth";
    public static final String HF_GNB_CONFIG_MYINFO_PROFILE = HF_GNB_CONFIG_MYINFO + "/profile";

    public static final String HF_GNB_CONFIG_PROJECT_ID = HF_GNB_CONFIG + "/project/%s";

    public static final String HF_GNB_CONFIG_HELPDESK = HF_GNB_CONFIG + "/helpdesk";

    private static FirebaseAnalytics mFirebaseAnalytics;

    public static void logEvent(@NonNull FirebaseAnalyticsInterface analyticsClassInfo) {
//        if (BuildConfig.DEBUG == false) {
            mFirebaseAnalytics = FirebaseAnalytics.getInstance(analyticsClassInfo.getClassContext());
//            mFirebaseAnalytics.logEvent(KEY_HF_TEST_EVENT, bundle);
            mFirebaseAnalytics.logEvent(BuildConfig.HF_KEY_GA_LOG_EVENT, makeBundle(analyticsClassInfo, null));
//        }
    }

    public static void logEvent(@NonNull FirebaseAnalyticsInterface analyticsClassInfo, String uri) {
//        if (BuildConfig.DEBUG == false) {
            mFirebaseAnalytics = FirebaseAnalytics.getInstance(analyticsClassInfo.getClassContext());
//        mFirebaseAnalytics.logEvent(KEY_HF_TEST_EVENT, bundle);
            mFirebaseAnalytics.logEvent(BuildConfig.HF_KEY_GA_LOG_EVENT, makeBundle(analyticsClassInfo, uri));
//        }
    }

    private static Bundle makeBundle(@NonNull FirebaseAnalyticsInterface analyticsClassInfo, @Nullable String uri) {
        Context context = analyticsClassInfo.getClassContext();
        String classUri = BASE_URI + ((uri != null && !uri.isEmpty()) ? uri : analyticsClassInfo.getClassUri());
        String className = analyticsClassInfo.getClassName();

        Bundle bundle = new Bundle();
        bundle.putString(KEY_HF_OS_VERSION, HFCommonUtil.getDeviceOsVersion());
        bundle.putString(KEY_HF_APP_VERSION, HFCommonUtil.getAppVersion(context));
        bundle.putString(KEY_HF_DEVICE_NAME, HFCommonUtil.getDeviceModelName());
        bundle.putString(KEY_HF_URI, classUri);
        bundle.putString(KEY_HF_CLASS_NAME, className);
//        Log.d(TAG, "logEvent - " + bundle.toString());
        return bundle;
    }

}
/*
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id);
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name);
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image");
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

Bundle params = new Bundle();
params.putString("image_name", name);
params.putString("full_text", text);
mFirebaseAnalytics.logEvent("share_image", params);
*/
