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

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

import java.util.ArrayList;

import kr.co.hanwha.hifive.data.model.AlarmData;
import kr.co.hanwha.hifive.data.model.AlarmListRoot;
import kr.co.hanwha.hifive.data.source.contract.AlarmDataSource;
import kr.co.hanwha.hifive.network.HFNetworkUtil;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;

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

public class AlarmDataRepository implements AlarmDataSource {
    private static ArrayList<AlarmData> sListAlarm;

    private static AlarmDataRepository sInstance = null;

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

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

    @Override
    public void clearData() {
        if (sListAlarm != null) sListAlarm.clear();
        sListAlarm = null;
    }

    @Override
    public void getAlarmList(@NonNull Context context, @NonNull int page, @NonNull final HanwhaNetworkCallback<AlarmListRoot> callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestNotificationList(context, encryption_member_id, page, new HanwhaNetworkCallback<AlarmListRoot>() {
            @Override
            public void onSuccess(AlarmListRoot response) {
                callback.onSuccess(response);
            }

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

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

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

    @Override
    public void requestNotificationOpen(@NonNull Context context, @NonNull AlarmData data, @Nullable HanwhaNetworkCallback callback) {
        requestNotificationOpen(context, data.getGroup_id(), data.getTask_id(), data.getProject_id(), data.getTrack_id(), callback);
    }

    @Override
    public void requestNotificationOpen(@NonNull Context context,
                                        @Nullable int group_id,
                                        @Nullable int task_id,
                                        @Nullable int project_id,
                                        @Nullable int track_id,
                                        @Nullable final HanwhaNetworkCallback callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestNotificationOpen(context, encryption_member_id, group_id, task_id, project_id, track_id, new HanwhaNetworkCallback<AlarmListRoot>() {
            @Override
            public void onSuccess(AlarmListRoot response) {
                if (callback != null) callback.onSuccess(null);
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                if (callback != null) callback.onFailure(error);
            }

            @Override
            public void onPreExecute() {
                if (callback != null) callback.onPreExecute();
            }

            @Override
            public void onPostExecute() {
                if (callback != null) callback.onPostExecute();
            }
        });
    }
}
