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

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

import kr.co.hanwha.hifive.data.model.MainHomeAssignedRoot;
import kr.co.hanwha.hifive.data.model.MainHomeRequestRoot;
import kr.co.hanwha.hifive.data.source.contract.TaskListDataSource;
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 TaskListDataRepository implements TaskListDataSource {
    private static TaskListDataRepository sInstance = null;

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

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

    @Override
    public void clearData() {

    }

    @Override
    public void requestTasksGetMainAssigned(@NonNull Context context,
                                            int selectMemberId,
                                            @NonNull final HanwhaNetworkCallback<MainHomeAssignedRoot> callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();



        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestTasksGetMainAssigned(context, encryption_member_id, selectMemberId, new HanwhaNetworkCallback<MainHomeAssignedRoot>() {
            @Override
            public void onSuccess(MainHomeAssignedRoot 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();
            }
        });
    }

    @Override
    public void requestTasksGetMainRequest(@NonNull Context context,
                                           int selectMemberId,
                                           @NonNull final HanwhaNetworkCallback<MainHomeRequestRoot> callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();

        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestTasksGetMainRequest(context, encryption_member_id, selectMemberId, new HanwhaNetworkCallback<MainHomeRequestRoot>() {
            @Override
            public void onSuccess(MainHomeRequestRoot 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();
            }
        });
    }

}
