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.MemberInfoData;
import kr.co.hanwha.hifive.data.model.MemberInfoRoot;
import kr.co.hanwha.hifive.data.source.contract.TaskMemberDataSource;
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 TaskMemberDataRepository implements TaskMemberDataSource {
    private static TaskMemberDataRepository sInstance = null;

    private static ArrayList<MemberInfoData> sListMemberInfo;
    private static MemberInfoData sSelectedMemberInfo;
    private static MemberInfoData sUnverifiedMemberInfoData;

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

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

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

        sSelectedMemberInfo = null;
        sUnverifiedMemberInfoData = null;
    }

    @Override
    public void setSelectMember(MemberInfoData data) {
        sUnverifiedMemberInfoData = data;
    }

    @Override
    public void setUnverifiedMemberInfo() {
        sUnverifiedMemberInfoData = sSelectedMemberInfo;
    }

    @Override
    public void setVerifyMemberInfo() {
        sSelectedMemberInfo = sUnverifiedMemberInfoData;
    }

    @Override
    public MemberInfoData getUnverifyMemberInfo() {
        return sUnverifiedMemberInfoData;
    }

    @Override
    public int getSelectedMemberid() {
        if (sSelectedMemberInfo != null)
            return sSelectedMemberInfo.getMember_id();
        else return MemberDataRepository.getInstance().getMemberId();
    }

    @Override
    public ArrayList<MemberInfoData> getSelectedMemberList() {
        ArrayList<MemberInfoData> list = new ArrayList<>();
        if (sSelectedMemberInfo != null) list.add(sSelectedMemberInfo);
        return list;
    }

    @Override
    public ArrayList<MemberInfoData> getUnverifySelectedMemberList() {
        ArrayList<MemberInfoData> list = new ArrayList<>();
        if (sUnverifiedMemberInfoData != null) list.add(sUnverifiedMemberInfoData);
        return list;
    }

    @Override
    public void requestAllMember(@NonNull Context context,
                                 @NonNull final HanwhaNetworkCallback callback) {
        if (sListMemberInfo == null || sListMemberInfo.size() == 0) {
            String encryption_member_id = MemberDataRepository.getInstance().getEncryptionMemberId();
            HFNetworkUtil networkUtil = new HFNetworkUtil();
            networkUtil.requestMemberAll(context, encryption_member_id, new HanwhaNetworkCallback<MemberInfoRoot>() {
                @Override
                public void onSuccess(MemberInfoRoot response) {
                    if (response != null) {
                        if (sListMemberInfo == null)
                            sListMemberInfo = new ArrayList<>();

                        for (int i = 0; i < response.getListMemberInfo().size(); i++) {
                            MemberInfoData data = response.getListMemberInfo().get(i);
                            if (data.getMember_id() == MemberDataRepository.getInstance().getMemberId())
                                sListMemberInfo.add(data);
                        }
                        boolean topLineAdd = false;
                        for (int i = 0; i < response.getListMemberInfo().size(); i++) {
                            MemberInfoData data = response.getListMemberInfo().get(i);
                            if (data.getMember_id() != MemberDataRepository.getInstance().getMemberId()) {
                                if (topLineAdd == false) {
                                    topLineAdd = true;
                                    data.setTopLine(true);
                                }
                                sListMemberInfo.add(data);
                            }
                        }
                        callback.onSuccess(sListMemberInfo);
                    }
                }

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

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

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

}
