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.R;
import kr.co.hanwha.hifive.adapter.contract.MainHomeAssignedListContract;
import kr.co.hanwha.hifive.adapter.contract.MainHomeRequestListContract;
import kr.co.hanwha.hifive.data.model.BaseData;
import kr.co.hanwha.hifive.data.model.MainHomeAssignedRoot;
import kr.co.hanwha.hifive.data.model.MainHomeRequestRoot;
import kr.co.hanwha.hifive.data.model.TaskData;
import kr.co.hanwha.hifive.data.model.TaskDetailRoot;
import kr.co.hanwha.hifive.data.model.TaskListData;
import kr.co.hanwha.hifive.data.source.TaskDataRepository;
import kr.co.hanwha.hifive.dialog.HFTaskInputDialog;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;

/**
 * 메인 - HOME Presenter
 *
 * @see MainHomeFragment
 * @see TaskDataRepository
 * Created by sanghoonkwon on 2018. 1. 25..
 */

public class MainHomePresenter implements MainHomeContract.Presenter {
    private Context mContext;
    private Activity mActivity;
    private MainHomeContract.View mView;
    private MainHomeAssignedListContract.View mAdapterViewAssigned;
    private MainHomeAssignedListContract.Model mAdapterModelAssigned;
    private MainHomeRequestListContract.View mAdapterViewRequest;
    private MainHomeRequestListContract.Model mAdapterModelRequest;

    private boolean isInitRequest = false;

    public MainHomePresenter(@NonNull Context context,
                             @NonNull MainHomeContract.View view,
                             @NonNull MainHomeAssignedListContract.View adapterViewAssigned,
                             @NonNull MainHomeAssignedListContract.Model adapterModelAssigned,
                             @NonNull MainHomeRequestListContract.View adapterViewRequest,
                             @NonNull MainHomeRequestListContract.Model adapterModelRequest) {
        mContext = context;
        mActivity = (Activity) context;
        mView = view;
        mAdapterViewAssigned = adapterViewAssigned;
        mAdapterModelAssigned = adapterModelAssigned;
        mAdapterViewRequest = adapterViewRequest;
        mAdapterModelRequest = adapterModelRequest;
    }

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

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

    @Override
    public void refreshData() {
        isInitRequest = false;
        initData();
    }

    @Override
    public void initData() {
        initDataAssigned(true);
    }

    @Override
    public void initDataAssigned(@NonNull final boolean needLoading) {
        TaskDataRepository.getInstance().requestTasksGetMainAssigned(getContext(), new HanwhaNetworkCallback<MainHomeAssignedRoot>() {
            @Override
            public void onSuccess(MainHomeAssignedRoot response) {

                if (response != null) {
                    ArrayList<TaskListData> listOfToday = response.getListTodayAssignedTasks();
                    ArrayList<TaskListData> listOfTasks = new ArrayList<>();
                    ArrayList<TaskListData> listOfClosed = response.getListClosedTasks();
                    listOfTasks.addAll(response.getListWaitingTasks());
                    listOfTasks.addAll(response.getListNotTodayAssignedTasks());
                    mAdapterModelAssigned.addItems(listOfToday, listOfTasks, listOfClosed);
                }
                mAdapterViewAssigned.notifyDataAll();
            }

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

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

            @Override
            public void onPostExecute() {
                mView.stopRefreshAssigned();
                mView.showLoadingDialog(false);
                if (isInitRequest == false) {
                    initDataRequested(needLoading);
                }
            }
        });
    }

    @Override
    public void initDataRequested(@NonNull final boolean needLoading) {
        TaskDataRepository.getInstance().requestTasksGetMainRequest(getContext(), new HanwhaNetworkCallback<MainHomeRequestRoot>() {
            @Override
            public void onSuccess(MainHomeRequestRoot response) {

                if (response != null) {
                    ArrayList<TaskListData> listOfEvaluation = new ArrayList<>();
                    listOfEvaluation.addAll(response.getListResolvedRequestTasks());
                    listOfEvaluation.addAll(response.getListRejectedRequestTasks());
                    ArrayList<TaskListData> listOfRequested = response.getListRequestTasks();
                    mAdapterModelRequest.addItems(listOfEvaluation, listOfRequested);
                }
                mAdapterViewRequest.notifyDataAll();
            }

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

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

            @Override
            public void onPostExecute() {
                isInitRequest = true;
                mView.stopRefreshRequest();
                mView.showLoadingDialog(false);
            }
        });
    }

    @Override
    public void updateTodoTaskToTodayTask(@NonNull TaskData data, boolean isChecked) {
        TaskDataRepository.getInstance().requestSetTaskToday(getContext(), data, isChecked, assignedHanwhaNetworkCallback);
    }

    @Override
    public void doTaskAccept(@NonNull TaskData data) {
        TaskDataRepository.getInstance().requestTaskAccept(getContext(), data, new HanwhaNetworkCallback<BaseData>() {
            @Override
            public void onSuccess(BaseData response) {
                initDataAssigned(true);
            }

            @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 doTaskReject(@NonNull final TaskData data) {
        new HFTaskInputDialog(getActivity(), R.string.task_reject_title, new HFTaskInputDialog.OnDialogCallback() {
            @Override
            public void onPositive(@NonNull final String message) {
                TaskDataRepository.getInstance().requestTaskReject(getContext(),
                        data, message, new HanwhaNetworkCallback<BaseData>() {
                            @Override
                            public void onSuccess(BaseData response) {
                                initDataAssigned(true);
                            }

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

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

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

    }

    /**************************************************
     * HanwhaNetworkCallback
     **************************************************/
    HanwhaNetworkCallback assignedHanwhaNetworkCallback = new HanwhaNetworkCallback() {
        @Override
        public void onSuccess(Object response) {
            initDataAssigned(true);
        }

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

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

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

    private Activity getActivity() {
        return mActivity;
    }
}
