package kr.co.hanwha.hifive.util;

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

import java.util.ArrayList;

import kr.co.hanwha.hifive.data.model.NoticeData;

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

public class HFInfoUtil {
    /**************************************************
     * 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);
    }

}
