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

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

import java.util.ArrayList;

import kr.co.hanwha.hifive.adapter.contract.SearchTaskListContract;
import kr.co.hanwha.hifive.data.model.ProjectInfoData;
import kr.co.hanwha.hifive.data.model.SearchTagData;
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.util.HFInfoUtil;

/**
 * 검색 Presenter
 *
 * @see SearchActivity
 * Created by sanghoonkwon on 2018. 1. 23..
 */

public class SearchPresenter implements SearchContract.Presenter {
    private SearchContract.View mView;
    private SearchTaskListContract.Model mListModel;
    private SearchTaskListContract.View mListView;

    public SearchPresenter(@NonNull SearchContract.View view,
                           @NonNull SearchTaskListContract.Model listModel,
                           @NonNull SearchTaskListContract.View listView) {
        mView = view;
        mListModel = listModel;
        mListView = listView;
    }

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

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

    @Override
    public void initRecentKeyword() {
        ArrayList<String> list = HFInfoUtil.getListRecentKeyword(getContext());
        mView.setRecentKeyword(list);
    }

    @Override
    public void initProjectList() {
        TaskDataRepository.getInstance().requestTeamModuleList(getContext(), new HanwhaNetworkCallback<ArrayList<ProjectInfoData>>() {
            @Override
            public void onSuccess(ArrayList<ProjectInfoData> response) {
                ArrayList<String> list = new ArrayList<>();
                if (response != null) {
                    for (int i = 0; i < response.size(); i++) {
                        list.add(response.get(i).getProject_name());
                    }
                }
                mView.setProjectKeyword(list);
            }

            @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 doSearch(@NonNull String text) {
        if (TextUtils.isEmpty(text) == false) {
            HFInfoUtil.setRecentKeyword(getContext(), text);
//            initRecentKeyword();

            TaskDataRepository.getInstance().requestSearchTaskByTag(getContext(), text, new HanwhaNetworkCallback<SearchTagData>() {
                @Override
                public void onSuccess(SearchTagData response) {
                    int count = 0;
                    if (response != null) {
                        mListModel.addItems(response.getResultListTasks());

                        if (response.getResultListTasks() != null)
                            count = response.getResultListTasks().size();
                    }

                    mListView.notifyDataAll();
                    mView.setSearchResult(count);
                }

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

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

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