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.R;
import kr.co.hanwha.hifive.adapter.contract.AddTaskSelectCampaignListContract;
import kr.co.hanwha.hifive.data.model.CampaignData;
import kr.co.hanwha.hifive.data.model.MemberInfoData;
import kr.co.hanwha.hifive.data.model.ProjectCampaignData;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
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.task.contract.AddTaskSelectCampaignContract;

/**
 * Created by sanghoonkwon on 2018. 2. 21..
 */

public class AddTaskSelectCampaignPresenter implements AddTaskSelectCampaignContract.Presenter {
    private AddTaskSelectCampaignContract.View mView;
    private AddTaskSelectCampaignListContract.Model mAdapterModel;
    private AddTaskSelectCampaignListContract.View mAdapterView;

    private String mKeyword = "";

    public AddTaskSelectCampaignPresenter(@NonNull AddTaskSelectCampaignContract.View view,
                                          @NonNull AddTaskSelectCampaignListContract.Model adapterModel,
                                          @NonNull AddTaskSelectCampaignListContract.View adapterView) {
        mView = view;
        mAdapterModel = adapterModel;
        mAdapterView = adapterView;
    }

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

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

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

    @Override
    public void initData() {
        TaskDataRepository.setUnverifiedCampaignInfo();
        mAdapterModel.setSelectedCampaignKey(TaskDataRepository.getVerifiedCampaignKey());

        ArrayList<Integer> listMemberInfo = new ArrayList<>();
        // 선택한 멤버들의 ID
        ArrayList<MemberInfoData> listSelected = TaskDataRepository.getSelectedMemberList();
        if (listSelected != null) {
            for (MemberInfoData data : listSelected) {
                listMemberInfo.add(data.getMember_id());
            }
        }

        if (listMemberInfo.size() == 0) {
            // 내 MemberId
            int memberId = MemberDataRepository.getInstance().getMemberId();
            if (memberId != 0)
                listMemberInfo.add(memberId);
        }

        // 선택 되어 있는 멤버 정보
        MemberInfoData info = TaskDataRepository.getMemberInfoData();
        if (info != null) {
            listMemberInfo.add(info.getMember_id());
        }

        if (listMemberInfo.size() == 0) {
            mView.showAlert(getContext().getString(R.string.alert_default_title), getContext().getString(R.string.main_project_empty),
                    new HFSingleDialog.OnDialogCallback() {
                        @Override
                        public void onPositive() {
                            getActivity().finish();
                        }
                    });
            return;
        }

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

                if (response == null || response.size() == 0) {
                    mView.showAlert(getContext().getString(R.string.alert_default_title), getContext().getString(R.string.main_project_empty),
                            new HFSingleDialog.OnDialogCallback() {
                                @Override
                                public void onPositive() {
                                    getActivity().finish();
                                }
                            });
                }
            }

            @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 CampaignData data) {
        TaskDataRepository.setCampaignListSelect(data);

        mAdapterModel.setSelectedCampaignKey(data.getKey());
        mAdapterView.notifyDataAll();
        doSearchKeyword(mKeyword);
    }

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

    @Override
    public void doSearchKeyword(@Nullable String keyword) {
        mKeyword = keyword;
        mAdapterModel.getFilter().filter(keyword);
    }
}
