package kr.co.hanwha.hifive.screen.main.home;

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

import java.util.ArrayList;

import kr.co.hanwha.hifive.adapter.contract.MainOKRListContract;
import kr.co.hanwha.hifive.data.model.ProjectCampaignData;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.data.source.TaskCampaignDataRepository;
import kr.co.hanwha.hifive.data.source.TaskDataRepository;
import kr.co.hanwha.hifive.dialog.HFSingleDialog;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.screen.main.home.contract.MainOKRContract;

/**
 * 개인 OKR 목록 조회 Presenter
 *
 * @see MainOKRActivity
 */
public class MainOKRPresenter implements MainOKRContract.Presenter {
    private MainOKRContract.View mView;
    private MainOKRListContract.Model mAdapterModel;
    private MainOKRListContract.View mAdapterView;

    public MainOKRPresenter(@NonNull MainOKRContract.View view,
                            @NonNull MainOKRListContract.Model adapterModel,
                            @NonNull MainOKRListContract.View adapterView) {
        mView = view;
        mAdapterModel = adapterModel;
        mAdapterView = adapterView;
    }

    @Override
    public void detachView() {
        mView = null;
        mAdapterModel = null;
        mAdapterView = null;

        TaskCampaignDataRepository.getInstance().clearData();
        TaskCampaignDataRepository.destroyInstance();
    }

    @Override
    public void initData(boolean needLoading) {
        ArrayList<Integer> listMemberInfo = new ArrayList<>();
        int memberId = MemberDataRepository.getInstance().getMemberId();// 내 MemberId
        if (memberId != 0)
            listMemberInfo.add(memberId);

        TaskCampaignDataRepository.getInstance().requestCampaignListByMemberId(getContext(), listMemberInfo, new HanwhaNetworkCallback<ArrayList<ProjectCampaignData>>() {
            @Override
            public void onSuccess(ArrayList<ProjectCampaignData> response) {
                mAdapterModel.addItems(response);
                mAdapterView.notifyDataAll();

                mView.setEmptyView(response == null || response.size() == 0);
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                mView.showAlert(error, new HFSingleDialog.OnDialogCallback() {
                    @Override
                    public void onPositive() {
                        getActivity().finish();
                    }
                });
            }

            @Override
            public void onPreExecute() {
                mView.showLoadingDialog(true);
            }

            @Override
            public void onPostExecute() {
                mView.showLoadingDialog(false);
                mView.setRefreshing(false);
            }
        });
    }

    @Override
    public Context getContext() {
        return (Context) mView;
    }

    public Activity getActivity() {
        return (Activity) mView;
    }
}
