package kr.co.hanwha.hifive.util;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Calendar;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.data.model.NoticeData;
import kr.co.hanwha.hifive.data.model.NotificationData;
import kr.co.hanwha.hifive.enums.MainTab;

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

public class HFInfoUtil {
    /**************************************************
     * 소속 선택(2018.5.11) - Default 생명
     **************************************************/
    public static String getDepartmentValue(@NonNull Context context) {
        String department = HFSharedPreferences.getString(context, HFSharedPreferences.KEY_SELECTED_DEPARTMENT);
        if (TextUtils.isEmpty(department)) department = HFConstants.USER_TYPE_LF;
        return department;
    }

    public static void setDepartmentValue(@NonNull Context context, @NonNull String value) {
        HFSharedPreferences.setString(context, HFSharedPreferences.KEY_SELECTED_DEPARTMENT, value);
    }

    public static String getDepartmentText(@NonNull Context context) {
        String text = "";
        String value = getDepartmentValue(context);
        if (HFConstants.USER_TYPE_LF.equals(value))
            text = context.getString(R.string.department_life);
        else if (HFConstants.USER_TYPE_SNC.equals(value))
            text = context.getString(R.string.department_snc);
        return text;
    }

    public static String getDepartmentCode(@NonNull Context context) {
        String code = "";
        String value = getDepartmentValue(context);
        if (HFConstants.USER_TYPE_LF.equals(value))
            code = HFConstants.USER_TYPE_LF_CODE;
        else if (HFConstants.USER_TYPE_SNC.equals(value))
            code = HFConstants.USER_TYPE_SNC_CODE;
        return code;
    }

    /**************************************************
     * 간편 비밀번호 확인 (1분 이상 지났을때만. 2018.4.12)
     **************************************************/
    private final static long checkTime = 1000 * 60;

    public static boolean checkAuthTime(@NonNull Context context) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        long currentTime = calendar.getTime().getTime();
        long lastTime = HFSharedPreferences.getLong(context, HFSharedPreferences.KEY_CHECK_AUTH_TIME);

        boolean check = lastTime + checkTime < currentTime;
        return check;
    }

    public static void setLastCheckAuthTime(@NonNull Context context) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        long time = calendar.getTime().getTime();

        HFSharedPreferences.setLong(context, HFSharedPreferences.KEY_CHECK_AUTH_TIME, time);
    }

    /**************************************************
     * Default Screen
     **************************************************/
    public static MainTab getDefaultScreen(@NonNull Context context) {
        int defaultTab = HFSharedPreferences.getInt(context, HFSharedPreferences.KEY_DEFAULT_SCREEN);
        if (defaultTab == MainTab.HOME.getCode()) return MainTab.HOME;
        else if (defaultTab == MainTab.PROJECT.getCode()) return MainTab.PROJECT;
        else if (defaultTab == MainTab.DASHBOARD.getCode()) return MainTab.DASHBOARD;
        else if (defaultTab == MainTab.SETTING.getCode()) return MainTab.SETTING;

        return MainTab.HOME;
    }

    public static void setDefaultScreen(@NonNull Context context, @NonNull MainTab tab) {
        HFSharedPreferences.setInt(context, HFSharedPreferences.KEY_DEFAULT_SCREEN, tab.getCode());
    }

    /**************************************************
     * Notification
     **************************************************/
    public static NotificationData getNotificationData(@NonNull Context context) {
        String noti_info_id = HFSharedPreferences.getString(context, HFSharedPreferences.KEY_NOTIFICATION_GROUP_ID);
        String title = HFSharedPreferences.getString(context, HFSharedPreferences.KEY_NOTIFICATION_TITLE);
        String message = HFSharedPreferences.getString(context, HFSharedPreferences.KEY_NOTIFICATION_MESSAGE);
        String type = HFSharedPreferences.getString(context, HFSharedPreferences.KEY_NOTIFICATION_TYPE);
        String value1 = HFSharedPreferences.getString(context, HFSharedPreferences.KEY_NOTIFICATION_VALUE_1);
        String value2 = HFSharedPreferences.getString(context, HFSharedPreferences.KEY_NOTIFICATION_VALUE_2);
        String value3 = HFSharedPreferences.getString(context, HFSharedPreferences.KEY_NOTIFICATION_VALUE_3);
        String db_code = HFSharedPreferences.getString(context, HFSharedPreferences.KEY_NOTIFICATION_DB_CODE);
        NotificationData data = null;
        if (TextUtils.isEmpty(type) == false) {
            data = new NotificationData(noti_info_id, title, message, type, value1, value2, value3, db_code);
        }
        return data;
    }

    public static void clearNotificationData(@NonNull Context context) {
        setNotificationData(context, null);

    }

    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());
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_DB_CODE, data.getDb_code());
        } 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);
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTIFICATION_DB_CODE, null);
        }
    }

    /**************************************************
     * Refresh
     **************************************************/
    public static boolean getNeedRefreshAlarmList(@NonNull Context context) {
        boolean need = HFSharedPreferences.getBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_ALARM_LIST);
        HFSharedPreferences.setBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_ALARM_LIST, false);
        return need;
    }

    public static void setNeedRefreshAlarmList(@NonNull Context context) {
        HFSharedPreferences.setBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_ALARM_LIST, true);
    }

    public static boolean getNeedRefreshMainHomeListAssigned(@NonNull Context context) {
        boolean need = HFSharedPreferences.getBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_MAIN_HOME_ASSIGNED);
        HFSharedPreferences.setBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_MAIN_HOME_ASSIGNED, false);
        return need;
    }

    public static void setNeedRefreshMainHomeListAssigned(@NonNull Context context) {
        HFSharedPreferences.setBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_MAIN_HOME_ASSIGNED, true);
    }

    public static boolean getNeedRefreshMainHomeListRequest(@NonNull Context context) {
        boolean need = HFSharedPreferences.getBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_MAIN_HOME_REQUEST);
        HFSharedPreferences.setBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_MAIN_HOME_REQUEST, false);
        return need;
    }

    public static void setNeedRefreshMainHomeListRequest(@NonNull Context context) {
        HFSharedPreferences.setBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_MAIN_HOME_REQUEST, true);
    }

    public static boolean getNeedRefreshMainProjectList(@NonNull Context context) {
        boolean need = HFSharedPreferences.getBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_MAIN_PROJECT);
        HFSharedPreferences.setBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_MAIN_PROJECT, false);
        return need;
    }

    public static void setNeedRefreshMainProjectList(@NonNull Context context) {
        HFSharedPreferences.setBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_MAIN_PROJECT, true);
    }

    public static boolean getNeedRefreshMainDashboardList(@NonNull Context context) {
        boolean need = HFSharedPreferences.getBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_MAIN_DASHBOARD);
        HFSharedPreferences.setBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_MAIN_DASHBOARD, false);
        return need;
    }

    public static void setNeedRefreshMainDashboardList(@NonNull Context context) {
        HFSharedPreferences.setBoolean(context, HFSharedPreferences.KEY_NEED_REFRESH_MAIN_DASHBOARD, true);
    }


    /**************************************************
     * UUID
     **************************************************/
    public static String getDeviceUUID(@NonNull Context context) {
        return HFSharedPreferences.getString(context, HFSharedPreferences.KEY_DEVICE_UUID);
    }

    public static void setDeviceUUID(@NonNull Context context, @NonNull String uuid) {
        HFSharedPreferences.setString(context, HFSharedPreferences.KEY_DEVICE_UUID, uuid);
    }

    /**************************************************
     * 간편 로그인 아이디
     **************************************************/
    public static String getMemberLoginId(@NonNull Context context) {
        return HFSharedPreferences.getString(context, HFSharedPreferences.KEY_MEMBER_LOGIN_ID);
    }

    public static void setMemberLoginId(@NonNull Context context, @NonNull String loginId) {
        HFSharedPreferences.setString(context, HFSharedPreferences.KEY_MEMBER_LOGIN_ID, loginId);
    }

    /**************************************************
     * 마지막 로그인 아이디 저장
     **************************************************/
    public static String getLastMemberLoginId(@NonNull Context context) {
        return HFSharedPreferences.getString(context, HFSharedPreferences.KEY_LAST_MEMBER_LOGIN_ID);
    }

    public static void setLastMemberLoginId(@NonNull Context context, @NonNull String loginId) {
        HFSharedPreferences.setString(context, HFSharedPreferences.KEY_LAST_MEMBER_LOGIN_ID, loginId);
    }

    /**************************************************
     * CHECK NOTICE
     **************************************************/
    public static int getLastNoticeNo(@NonNull Context context) {
        return HFSharedPreferences.getInt(context, HFSharedPreferences.KEY_LAST_NOTICE_NO);
    }

    public static void setNoticeNo(@NonNull Context context, @NonNull int notice_no) {
        HFSharedPreferences.setInt(context, HFSharedPreferences.KEY_LAST_NOTICE_NO, notice_no);
    }

    /**************************************************
     * NOTICE INFO
     **************************************************/
    public static NoticeData getNoticeData(@NonNull Context context) {
        NoticeData data = null;
        int no = HFSharedPreferences.getInt(context, HFSharedPreferences.KEY_NOTICE_INFO_NO);
        String url = HFSharedPreferences.getString(context, HFSharedPreferences.KEY_NOTICE_INFO_URL);
        if (no != 0 && TextUtils.isEmpty(url) == false) {
            data = new NoticeData();
            data.setId(no);
            data.setUrl(url);

            setNoticeData(context, null);//Clear
        }
        return data;
    }

    public static void setNoticeData(@NonNull Context context, @NonNull NoticeData data) {
        if (data != null) {
            HFSharedPreferences.setInt(context, HFSharedPreferences.KEY_NOTICE_INFO_NO, data.getId());
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTICE_INFO_URL, data.getUrl());
        } else {
            HFSharedPreferences.setInt(context, HFSharedPreferences.KEY_NOTICE_INFO_NO, 0);
            HFSharedPreferences.setString(context, HFSharedPreferences.KEY_NOTICE_INFO_URL, "");
        }
    }

    /**************************************************
     * 최근 검색어
     **************************************************/
    public static ArrayList<String> getListRecentKeyword(@NonNull Context context) {
        return HFSharedPreferences.getStringArrayPref(context, HFSharedPreferences.KEY_RECENT_KEYWORD_LIST);
    }

    public static void setRecentKeyword(@NonNull Context context, @NonNull String keyword) {
        if (TextUtils.isEmpty(keyword)) return;

        int existIndex = -1;
        ArrayList<String> list = getListRecentKeyword(context);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                String value = list.get(i);
                if (keyword.equals(value)) {
                    existIndex = i;
                }
            }
            if (existIndex >= 0)
                list.remove(existIndex);
        }
        list.add(0, keyword);
        HFSharedPreferences.setStringArrayPref(context, HFSharedPreferences.KEY_RECENT_KEYWORD_LIST, list);
    }

    public static void deleteRecentKeyword(@NonNull Context context, @NonNull String keyword) {
        if (TextUtils.isEmpty(keyword)) return;

        int existIndex = -1;
        ArrayList<String> list = getListRecentKeyword(context);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                String value = list.get(i);
                if (keyword.equals(value)) {
                    existIndex = i;
                }
            }
            if (existIndex >= 0)
                list.remove(existIndex);
        }
        HFSharedPreferences.setStringArrayPref(context, HFSharedPreferences.KEY_RECENT_KEYWORD_LIST, list);
    }

}
