package kr.co.hanwha.hifive.data.source;

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

import kr.co.hanwha.hifive.data.model.SearchTagData;
import kr.co.hanwha.hifive.data.source.contract.SearchDataSource;
import kr.co.hanwha.hifive.network.HFNetworkUtil;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;

public class SearchDataRepository implements SearchDataSource {
    private static SearchDataRepository sInstance = null;

    public static SearchDataRepository getInstance() {
        if (sInstance == null) {
            sInstance = new SearchDataRepository();
        }
        return sInstance;
    }

    public static void destroyInstance() {
        sInstance = null;
    }

    @Override
    public void clearData() {

    }

    @Override
    public void requestSearchTaskByTag(@NonNull Context context,
                                       @NonNull String searchTag,
                                       @Nullable final HanwhaNetworkCallback<SearchTagData> callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestSearchTaskByTag(context, encryption_member_id, searchTag, new HanwhaNetworkCallback<SearchTagData>() {
            @Override
            public void onSuccess(SearchTagData response) {
                callback.onSuccess(response);
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                if (error != null && error.getStatusCode() == HanwhaNetworkError.CODE_EMPTY_DATA) {
                    callback.onSuccess(null);
                } else
                    callback.onFailure(error);
            }

            @Override
            public void onPreExecute() {
                callback.onPreExecute();
            }

            @Override
            public void onPostExecute() {
                callback.onPostExecute();
            }
        });
    }

}
