package kr.co.hanwha.hifive.screen.task;

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

import java.util.ArrayList;

import kr.co.hanwha.hifive.adapter.contract.AddTaskSelectMemberListContract;
import kr.co.hanwha.hifive.adapter.contract.AddTaskSelectedMemberPagerContract;
import kr.co.hanwha.hifive.data.model.MemberInfoData;
import kr.co.hanwha.hifive.data.model.ProjectInfoData;
import kr.co.hanwha.hifive.data.source.TaskDataRepository;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.screen.task.contract.AddTaskSelectMemberContract;

/**
 * Add TASK  : 멤버 선택 Presenter
 *
 * @see AddTaskSelectMemberActivity
 * Created by sanghoonkwon on 2018. 1. 18..
 */

public class AddTaskSelectMemberPresenter implements AddTaskSelectMemberContract.Presenter {
    private AddTaskSelectMemberContract.View mView;
    private AddTaskSelectMemberListContract.View mListAdapterView;
    private AddTaskSelectMemberListContract.Model mListAdapterModel;
    private AddTaskSelectedMemberPagerContract.View mPagerAdapterView;
    private AddTaskSelectedMemberPagerContract.Model mPagerAdapterModel;

    public AddTaskSelectMemberPresenter(@NonNull AddTaskSelectMemberContract.View view,
                                        @NonNull AddTaskSelectMemberListContract.View listAdapterView,
                                        @NonNull AddTaskSelectMemberListContract.Model listAdapterModel,
                                        @NonNull AddTaskSelectedMemberPagerContract.View pagerAdapterView,
                                        @NonNull AddTaskSelectedMemberPagerContract.Model pagerAdapterModel) {
        mView = view;
        mListAdapterView = listAdapterView;
        mListAdapterModel = listAdapterModel;
        mPagerAdapterView = pagerAdapterView;
        mPagerAdapterModel = pagerAdapterModel;
    }

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

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

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

    @Override
    public void initData() {
        TaskDataRepository.getInstance().requestAllMember(getContext(), new HanwhaNetworkCallback<ArrayList<MemberInfoData>>() {
            @Override
            public void onSuccess(ArrayList<MemberInfoData> response) {
                mListAdapterModel.addItems(response);
                mListAdapterView.notifyDataAll();
            }

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

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

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

    @Override
    public void updateObjectSelect(@NonNull MemberInfoData data, @NonNull int index, @NonNull boolean isSelect) {
        int idxList = TaskDataRepository.setMemberListSelect(data, isSelect);

        if (index != -1) {
            mListAdapterView.notifyDataRangeChanged(index, 1);
//            mListAdapterView.notifyDataRangeChanged(0, 1);
        } else {
            mListAdapterView.notifyDataAll();
        }

        refreshSelectedMember();
    }

    @Override
    public void refreshSelectedMember() {
        ArrayList<MemberInfoData> listSelected = TaskDataRepository.getSelectedDataList();
        mPagerAdapterModel.addItems(listSelected);
        mPagerAdapterView.notifyDataAll();
        mView.setSelectedMemberVisible(listSelected.size() > 0);
        mView.setCompleteCount(listSelected.size());
    }

    @Override
    public void doCompleted() {
        TaskDataRepository.setVerifyData();
        getActivity().setResult(Activity.RESULT_OK);
        getActivity().finish();
    }
}
