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

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

import java.util.ArrayList;

import kr.co.hanwha.hifive.data.model.DabDataRoot;
import kr.co.hanwha.hifive.data.model.MainProjectRoot;
import kr.co.hanwha.hifive.data.model.ProjectInfoData;
import kr.co.hanwha.hifive.data.model.ProjectInfoRoot;
import kr.co.hanwha.hifive.data.source.contract.ProjectDataSource;
import kr.co.hanwha.hifive.enums.ProjectEvaluationType;
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. 2. 20..
 */

public class ProjectDataRepository implements ProjectDataSource {
    private static ProjectDataRepository sInstance = null;

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

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


    @Override
    public void clearData() {
    }

    @Override
    public void requestProjectList(@NonNull Context context, @NonNull final HanwhaNetworkCallback<ArrayList<ProjectInfoData>> callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();

        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestProjectsGetMine(context, encryption_member_id, new HanwhaNetworkCallback<ProjectInfoRoot>() {
            @Override
            public void onSuccess(ProjectInfoRoot response) {
                if (response != null && response.getListProjectsMenus() != null) {
                    ArrayList<ProjectInfoData> list = new ArrayList<>();
                    for (int i = 0; i < response.getListProjectsMenus().size(); i++) {
                        ProjectInfoData data = response.getListProjectsMenus().get(i);
                        if (MemberDataRepository.getInstance().checkProjectId(data.getProject_id()))
                            list.add(data);
                    }
                    for (int i = 0; i < response.getListProjectsMenus().size(); i++) {
                        ProjectInfoData data = response.getListProjectsMenus().get(i);
                        if (MemberDataRepository.getInstance().checkProjectId(data.getProject_id()) == false)
                            list.add(data);
                    }
                    callback.onSuccess(list);
                } else {
                    callback.onSuccess(null);
                }
            }

            @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 requestTaskListByCampaign(@NonNull Context context, @NonNull int project_id, @NonNull boolean isAll, @NonNull final HanwhaNetworkCallback<MainProjectRoot> callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();

        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestProjectsGetView(context, encryption_member_id, project_id, isAll, new HanwhaNetworkCallback<MainProjectRoot>() {
            @Override
            public void onSuccess(MainProjectRoot 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 requestUpdateProjectColor(@NonNull Context context, @NonNull int project_id, @NonNull ProjectEvaluationType evaluationType, @NonNull final HanwhaNetworkCallback callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();

        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestProjectsUpdateEvaluation(context, encryption_member_id, project_id, evaluationType, new HanwhaNetworkCallback() {
            @Override
            public void onSuccess(Object response) {
                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 requestDashboardList(@NonNull Context context, @NonNull final HanwhaNetworkCallback<DabDataRoot> callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();

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