package kr.co.hanwha.hifive.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.data.model.AlarmData;
import kr.co.hanwha.hifive.data.model.NotificationData;
import kr.co.hanwha.hifive.data.source.AlarmDataRepository;
import kr.co.hanwha.hifive.enums.LandingType;

/**
 * Created by sanghoonkwon on 2018. 1. 22..
 */

public class HFNotiUtil {
    public static String KEY_GROUP_ID = "group_id";
    public static String KEY_TITLE = "title";
    public static String KEY_BODY = "body";
    public static String KEY_MESSAGE = "message";
    public static String KEY_TYPE = "type";
    public static String KEY_VALUE1 = "value1";
    public static String KEY_VALUE2 = "value2";
    public static String KEY_VALUE3 = "value3";
    public static String KEY_DB_CODE = "db_code";

    public static void pendingNotification(@NonNull Context context, NotificationData data) {
        if (data != null) {
            HFInfoUtil.clearNotificationData(context);

            int type = 0;
            try {
                if (TextUtils.isEmpty(data.getType()) == false)
                    type = Integer.parseInt(data.getType());
            } catch (Exception e) {
                e.printStackTrace();
            }

            int taskId = 0;
            int projectId = 0;
            int trackId = 0;
            if (LandingType.TASK_DETAIL.getCode() == type ||
                    LandingType.TASK_DETAIL_COMMENT.getCode() == type) {
                try {
                    if (TextUtils.isEmpty(data.getValue1()) == false)
                        taskId = Integer.parseInt(data.getValue1());
                    if (TextUtils.isEmpty(data.getValue2()) == false)
                        projectId = Integer.parseInt(data.getValue2());
                    if (TextUtils.isEmpty(data.getValue3()) == false)
                        trackId = Integer.parseInt(data.getValue3());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                HFActivityUtil.startTaskDetailActivity((Activity) context, taskId, projectId, trackId);
            } else if (LandingType.MAIN_UNIT.getCode() == type) {
                HFNotiUtil.setNotificationData(context, data);
                HFActivityUtil.startMainActivity((Activity) context);
            } else if (LandingType.MAIN_DASHBOARD_UNIT.getCode() == type) {
                if (TextUtils.isEmpty(data.getValue1()) == false)
                    projectId = Integer.parseInt(data.getValue1());
                HFActivityUtil.startDashboardProjectActivity((Activity) context, projectId);
            } else if (LandingType.WEBVIEW.getCode() == type) {
                String url = data.getValue1();
                HFActivityUtil.startCommonWebViewActivity((Activity) context, url);
            } else {
                HFActivityUtil.startAlarmActivity((Activity) context);
            }

            int group_id = 0;
            try {
                if (TextUtils.isEmpty(data.getGroup_id()) == false)
                    group_id = Integer.parseInt(data.getGroup_id());
            } catch (Exception e) {
                e.printStackTrace();
            }

            AlarmDataRepository.getInstance().requestNotificationOpen(context, group_id, taskId, projectId, trackId, null);
        }
    }

    public static void pendingNotification(@NonNull Context context, AlarmData data) {
        if (data != null) {
            int type = data.getContext_id();
            if (LandingType.TASK_DETAIL.getCode() == type ||
                    LandingType.TASK_DETAIL_COMMENT.getCode() == type) {
                int taskId = data.getTask_id();
                int projectId = data.getProject_id();
                int trackId = data.getTrack_id();
                HFActivityUtil.startTaskDetailActivity((Activity) context, taskId, projectId, trackId);
            } else if (LandingType.MAIN_UNIT.getCode() == type) {
                int projectId = data.getProject_id();
                HFActivityUtil.startDashboardProjectActivity((Activity) context, projectId);
            } else if (LandingType.MAIN_DASHBOARD_UNIT.getCode() == type) {
                int projectId = data.getProject_id();
                HFActivityUtil.startDashboardProjectActivity((Activity) context, projectId);
            } else if (LandingType.WEBVIEW.getCode() == type) {
                String url = data.getUrl();
                HFActivityUtil.startCommonWebViewActivity((Activity) context, url);
            } else {
                HFActivityUtil.startAlarmActivity((Activity) context);
            }
        }
    }

    public static void setNotificationData(@NonNull Context context, @NonNull NotificationData data) {
        if (data != null) {
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_GROUP_ID, data.getGroup_id());
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_TITLE, data.getTitle());
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_MESSAGE, data.getMessage());
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_TYPE, data.getType());
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_VALUE_1, data.getValue1());
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_VALUE_2, data.getValue2());
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_VALUE_3, data.getValue3());
        } else {
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_GROUP_ID, null);
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_TITLE, null);
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_MESSAGE, null);
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_TYPE, null);
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_VALUE_1, null);
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_VALUE_2, null);
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_VALUE_3, null);
        }
    }

    /**
     * 알림 목록 아이콘
     *
     * @param context    Context
     * @param context_id 랜딩 타입
     * @return 알림 목록 아이콘
     */
    public static Drawable getAlarmIconDrawable(Context context, int context_id) {
        if (context_id == LandingType.TASK_DETAIL.getCode()) {
            return context.getResources().getDrawable(R.drawable.alarm_icon_01);
        } else if (context_id == LandingType.TASK_DETAIL_COMMENT.getCode()) {
            return context.getResources().getDrawable(R.drawable.alarm_icon_02);
        } else if (context_id == LandingType.MAIN_SETTING_TBD.getCode()) {
            return context.getResources().getDrawable(R.drawable.alarm_icon_03);
        }

        return context.getResources().getDrawable(R.drawable.alarm_icon_01);
    }
}
