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.AddTaskRoot;
import kr.co.hanwha.hifive.data.model.CampaignData;
import kr.co.hanwha.hifive.data.model.HelpdeskDataRoot;
import kr.co.hanwha.hifive.data.model.MemberInfoData;
import kr.co.hanwha.hifive.data.model.ReqMemberInfo;
import kr.co.hanwha.hifive.data.model.TaskData;
import kr.co.hanwha.hifive.data.model.TaskDetailRoot;
import kr.co.hanwha.hifive.data.source.contract.TaskDataSource;
import kr.co.hanwha.hifive.network.HFNetworkUtil;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;

/**
 * TASK 관련 데이터 저장소
 * Created by sanghoonkwon on 2018. 1. 11..
 */

public class TaskDataRepository implements TaskDataSource {
    private static TaskDataRepository sInstance = null;

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

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

    @Override
    public void clearData() {

    }

    @Override
    public void requestSetTaskToday(@NonNull Context context,
                                    @NonNull TaskData taskData,
                                    @NonNull boolean isToday,
                                    @Nullable HanwhaNetworkCallback callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
        taskData.setIs_today(isToday);
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestTasksUpdateToday(context, taskData, encryption_member_id, callback);
    }

    @Override
    public void requestSendTask(@NonNull Context context,
                                @NonNull String task_name,
                                @NonNull String due_to,
                                @NonNull ArrayList<MemberInfoData> listMemberInfo,
                                @Nullable CampaignData campaignData,
                                @Nullable final HanwhaNetworkCallback callback) {
        ArrayList<ReqMemberInfo> listMemberId = new ArrayList<>();
        if (listMemberInfo != null) {
            for (MemberInfoData data : listMemberInfo) {
                listMemberId.add(new ReqMemberInfo(data.getMember_id(), data.getProject_id(), data.getTrack_id()));
            }
        }

        int author_member_id = MemberDataRepository.getInstance().getMemberId();
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestSendTask(context, author_member_id, encryption_member_id, task_name, due_to, listMemberId, campaignData, new HanwhaNetworkCallback<AddTaskRoot>() {
            @Override
            public void onSuccess(AddTaskRoot response) {
                callback.onSuccess(response);
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                callback.onFailure(error);
            }

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

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

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

                    @Override
                    public void onFailure(HanwhaNetworkError error) {
                        callback.onFailure(error);
                    }

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

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

    @Override
    public void requestUpdateTask(@NonNull Context context,
                                  @NonNull TaskData taskData,
                                  @Nullable CampaignData campaignData,
                                  @Nullable final HanwhaNetworkCallback callback) {
        ArrayList<MemberInfoData> list = new ArrayList<>();
        list.add(new MemberInfoData(taskData.getMember_id(),
                taskData.getMember_name(),
                taskData.getMember_profile_url(),
                taskData.getProject_id(),
                taskData.getProject_name()));

        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestUpdateTask(context, encryption_member_id, taskData, campaignData, callback);
    }

    @Override
    public void requestDeleteTask(@NonNull Context context,
                                  @NonNull TaskData taskData,
                                  @Nullable HanwhaNetworkCallback callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestTaskDelete(context, taskData, encryption_member_id, callback);
    }

    @Override
    public void requestHelpdeskDetails(@NonNull Context context,
                                       @Nullable HanwhaNetworkCallback<HelpdeskDataRoot> callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestMembersHelpdeskDetails(context, encryption_member_id, callback);
    }
}
