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

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

import java.util.ArrayList;

import kr.co.hanwha.hifive.data.model.CampaignData;
import kr.co.hanwha.hifive.data.model.CampaignDataRoot;
import kr.co.hanwha.hifive.data.model.CampaignTreeData;
import kr.co.hanwha.hifive.data.model.CampaignTreeRoot;
import kr.co.hanwha.hifive.data.model.MemberCampaignsTree;
import kr.co.hanwha.hifive.data.model.ProjectCampaignData;
import kr.co.hanwha.hifive.data.model.ProjectCampaignsTree;
import kr.co.hanwha.hifive.data.source.contract.TaskCampaignDataSource;
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 TaskCampaignDataRepository implements TaskCampaignDataSource {
    private static TaskCampaignDataRepository sInstance = null;

    private static ArrayList<ProjectCampaignData> sListCampaignInfo;
    private static String sUnverifiedCampaignKey;
    private static String sVerifiedCampaignKey;
    private static CampaignData sCampaignData;
    private static ArrayList<CampaignTreeData> sListCampaignTreeData;

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

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

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

        sUnverifiedCampaignKey = null;
        sVerifiedCampaignKey = null;
        sCampaignData = null;
    }

    @Override
    public String getVerifiedCampaignKey() {
        return sVerifiedCampaignKey;
    }

    @Override
    public void setUnverifiedCampaignInfo() {
        if (TextUtils.isEmpty(sVerifiedCampaignKey) == false)
            sUnverifiedCampaignKey = new String(sVerifiedCampaignKey);
    }

    @Override
    public void setVerifyCampaignInfo() {
        if (sUnverifiedCampaignKey == null) return;
        sVerifiedCampaignKey = new String(sUnverifiedCampaignKey);
    }

    @Override
    public void setVerifyCampaignInfo(String verifyCampaignKey) {
        sVerifiedCampaignKey = verifyCampaignKey;
    }

    @Override
    public void setCampaignListSelect(CampaignData data) {
        if (sListCampaignTreeData != null && data != null) {
            sUnverifiedCampaignKey = data.getKey();
        } else {
            sCampaignData = data;
        }
    }

    @Override
    public ProjectCampaignData getSelectedCampaignData() {
        if (sListCampaignTreeData != null && TextUtils.isEmpty(sVerifiedCampaignKey) == false) {
            for (CampaignTreeData projectData : sListCampaignTreeData) {
                int projectId = projectData.getProject_id();
                String projectName = projectData.getProject_name();
                if (projectData.getListProjectCampaignsTree() != null) {
                    for (ProjectCampaignsTree objectData : projectData.getListProjectCampaignsTree()) {
                        for (MemberCampaignsTree keyResultData : objectData.getListMemberCampaignsTree()) {
                            String dataKey = projectId + "_" + keyResultData.getMember_campaign_id();
                            if (dataKey.equals(sVerifiedCampaignKey)) return new ProjectCampaignData(new CampaignData(
                                    keyResultData.getMember_campaign_id(),
                                    keyResultData.getMember_campaign_desc(),
                                    projectId,
                                    projectName
                            ));
                        }
                    }
                }
//                if (data.getKey().equals(sVerifiedCampaignKey)) return data;
            }
        }

        if (sCampaignData != null) return new ProjectCampaignData(sCampaignData);

        return null;
    }

    @Override
    public void clearSelectedCampaignData() {
        sVerifiedCampaignKey = "";
        sUnverifiedCampaignKey = "";
        sCampaignData = null;
    }

    @Override
    public void requestCampaignListByProjectId(@NonNull Context context,
                                               @NonNull ArrayList<Integer> listReqProjectId,
                                               @NonNull final HanwhaNetworkCallback callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestCampaignListByProjectId(context, encryption_member_id,
                listReqProjectId,
                new HanwhaNetworkCallback<CampaignDataRoot>() {
                    @Override
                    public void onSuccess(CampaignDataRoot response) {
                        if (response != null) {
                            sListCampaignInfo = response.getListProjectsCampaignsInfo();
                        }

                        callback.onSuccess(sListCampaignInfo);
                    }

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

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

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

    @Override
    public void requestCampaignListByMemberId(@NonNull Context context,
                                              @NonNull ArrayList<Integer> listMemberInfo,
                                              @NonNull final HanwhaNetworkCallback callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestCampaignListByMemberId(context, encryption_member_id,
                listMemberInfo,
                new HanwhaNetworkCallback<CampaignDataRoot>() {
                    @Override
                    public void onSuccess(CampaignDataRoot response) {
                        if (response != null) {
                            sListCampaignInfo = response.getListProjectsCampaignsInfo();
                        }

                        callback.onSuccess(sListCampaignInfo);
                    }

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

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

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

    public void requestCampaignTreeByMemberId(@NonNull Context context,
                                              @NonNull ArrayList<Integer> listMemberInfo,
                                              @NonNull final HanwhaNetworkCallback callback) {
        String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestCampaignTreeByMemberId(context, encryption_member_id, listMemberInfo,
                new HanwhaNetworkCallback<CampaignTreeRoot>() {

                    @Override
                    public void onSuccess(CampaignTreeRoot response) {
                        if (response != null) {
                            sListCampaignTreeData = response.getTreeProjectMemberCampaigns();
                        }

                        callback.onSuccess(sListCampaignTreeData);
                    }

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

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

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

}
