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.MainHomeRequestListContract;
import kr.co.hanwha.hifive.adapter.contract.SettingMyHomeOkrListContract;
import kr.co.hanwha.hifive.adapter.contract.SettingMyHomeTodoListContract;
import kr.co.hanwha.hifive.data.model.BaseData;
import kr.co.hanwha.hifive.data.model.CampaignTreeData;
import kr.co.hanwha.hifive.data.model.MainHomeAssignedRoot;
import kr.co.hanwha.hifive.data.model.MainHomeRequestRoot;
import kr.co.hanwha.hifive.data.model.ProfileData;
import kr.co.hanwha.hifive.data.model.ProfileRoot;
import kr.co.hanwha.hifive.data.model.TaskData;
import kr.co.hanwha.hifive.data.model.TaskListData;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.data.source.MyHomeDataRepository;
import kr.co.hanwha.hifive.data.source.TaskCampaignDataRepository;
import kr.co.hanwha.hifive.data.source.TaskDataRepository;
import kr.co.hanwha.hifive.data.source.TaskListDataRepository;
import kr.co.hanwha.hifive.data.source.TaskStatusDataRepository;
import kr.co.hanwha.hifive.dialog.HFTaskInputDialog;
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.MainMyHomeContract;
import kr.co.hanwha.hifive.util.HFActivityUtil;

/**
 * Created by aaron on 2018. 10. 16.
 */
public class MainMyHomePresenter implements MainMyHomeContract.Presenter {

    private Context mContext;
    private Activity mActivity;
    private MainMyHomeContract.View mView;
    private SettingMyHomeTodoListContract.View mAdapterViewTodo;
    private SettingMyHomeTodoListContract.Model mAdapterModelTodo;
    private SettingMyHomeOkrListContract.Model mAdpaterModelOkr;
    private SettingMyHomeOkrListContract.View mAdpaterViewOkr;
    private MainHomeRequestListContract.View mAdapterViewRequest;
    private MainHomeRequestListContract.Model mAdapterModelRequest;

    public MainMyHomePresenter(@NonNull Context context,
                               @NonNull MainMyHomeContract.View view,
                               @NonNull SettingMyHomeTodoListContract.View adapterViewTodo,
                               @NonNull SettingMyHomeTodoListContract.Model adapterModelTodo,
                               @NonNull SettingMyHomeOkrListContract.View adapterViewOkr,
                               @NonNull SettingMyHomeOkrListContract.Model adapterModelOkr,
                               @NonNull MainHomeRequestListContract.View adapterViewRequest,
                               @NonNull MainHomeRequestListContract.Model adapterModelRequest) {
        this.mContext = context;
        this.mActivity = (Activity) context;
        this.mView = view;
        this.mAdapterViewTodo = adapterViewTodo;
        this.mAdapterModelTodo = adapterModelTodo;
        this.mAdpaterViewOkr = adapterViewOkr;
        this.mAdpaterModelOkr = adapterModelOkr;
        this.mAdapterViewRequest = adapterViewRequest;
        this.mAdapterModelRequest = adapterModelRequest;
    }

    @Override
    public void refreshData() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initDataOkr(int selectMemberId, @NonNull final boolean needLoading) {
        ArrayList<Integer> listMemberInfo = new ArrayList<>();
        if (selectMemberId != 0)
            listMemberInfo.add(selectMemberId);

        TaskCampaignDataRepository.getInstance().requestCampaignTreeByMemberId(getContext(), listMemberInfo, new HanwhaNetworkCallback<ArrayList<CampaignTreeData>>() {
            @Override
            public void onSuccess(ArrayList<CampaignTreeData> response) {
                if (response != null) {
                    mAdpaterModelOkr.addItems(response);
                }
                mAdpaterViewOkr.notifyDataAll();
            }

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

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

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

    @Override
    public void initDataTodo(int selectMemberId, @NonNull final boolean needLoading) {
        TaskListDataRepository.getInstance().requestTasksGetMainAssigned(getContext(), selectMemberId, 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());
                    mAdapterModelTodo.addItems(listOfToday, listOfTasks, listOfClosed);
                }
                mAdapterViewTodo.notifyDataAll();
            }

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

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

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

    @Override
    public void initDataRequest(int selectMemberId, final boolean needLoading) {
        TaskListDataRepository.getInstance().requestTasksGetMainRequest(getContext(), selectMemberId, new HanwhaNetworkCallback<MainHomeRequestRoot>() {
            @Override
            public void onSuccess(MainHomeRequestRoot response) {

                if (response != null) {
                    ArrayList<TaskListData> listOfEvaluation = response.getListResolvedRequestTasks();
                    ArrayList<TaskListData> listOfRequested = new ArrayList<>();
                    listOfRequested.addAll(response.getListRequestTasks());
                    listOfRequested.addAll(response.getListRejectedRequestTasks());
                    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 initDataProfile(int selectMemberId, final boolean needLoading) {
        MyHomeDataRepository.getInstance().requestMyHomeGetProfile(getContext(), selectMemberId, new HanwhaNetworkCallback<ProfileRoot>() {
            @Override
            public void onSuccess(ProfileRoot response) {
                if (response != null) {
                    ProfileData data = response.getMemberProfile();
                    mView.setProfile(data);
                    mView.setProfileUnit(data.getListMemberProjects());
                }
            }

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

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

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

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

    @Override
    public void doTaskAccept(@NonNull TaskData data) {
        TaskStatusDataRepository.getInstance().requestTaskAccept(getContext(), data, new HanwhaNetworkCallback<BaseData>() {
            @Override
            public void onSuccess(BaseData response) {
                initDataTodo(MemberDataRepository.getInstance().getMemberId(), 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) {
                TaskStatusDataRepository.getInstance().requestTaskReject(getContext(),
                        data, message, new HanwhaNetworkCallback() {
                            @Override
                            public void onSuccess(Object response) {
                                mView.showToast(getContext().getString(R.string.task_reject_completed));
                                initDataTodo(MemberDataRepository.getInstance().getMemberId(), 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();
    }

    @Override
    public void doEvalAcceptClicked(@NonNull TaskData data) {
        TaskStatusDataRepository.getInstance().requestSetTaskEvaluationAccept(getContext(), data, requestHanwhaNetworkCallback);
    }

    @Override
    public void doEvalRejectClicked(@NonNull final TaskData data) {
        new HFTaskInputDialog(getActivity(), R.string.task_eval_reject_title, new HFTaskInputDialog.OnDialogCallback() {
            @Override
            public void onPositive(@NonNull final String message) {
                TaskStatusDataRepository.getInstance().requestSetTaskEvaluationReject(getContext(),
                        data, message, new HanwhaNetworkCallback() {
                            @Override
                            public void onSuccess(Object response) {
                                mView.showToast(getContext().getString(R.string.task_eval_reject_completed));
                                initDataRequest(MemberDataRepository.getInstance().getMemberId(), 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();
    }

    @Override
    public void doEvalRevaluationClicked(@NonNull TaskData data) {
        mView.showToast(getContext().getString(R.string.task_eval_revaluation_only_web));
    }

    @Override
    public void doDeleteRequestClicked(@NonNull TaskData data) {
        TaskStatusDataRepository.getInstance().requestSetTaskDeleteRequest(getContext(), data, requestHanwhaNetworkCallback);
    }

    @Override
    public void doReRequestClicked(@NonNull TaskData data) {
        HFActivityUtil.startEditTaskAndReRequestActivity(getActivity(), data.getTask_id(), data.getProject_id());
    }

    /**************************************************
     * HanwhaNetworkCallback
     **************************************************/
    HanwhaNetworkCallback todoHanwhaNetworkCallback = new HanwhaNetworkCallback() {
        @Override
        public void onSuccess(Object response) {
            initDataTodo(MemberDataRepository.getInstance().getMemberId(), true);
        }

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

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

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

    HanwhaNetworkCallback requestHanwhaNetworkCallback = new HanwhaNetworkCallback() {
        @Override
        public void onSuccess(Object response) {
            initDataRequest(MemberDataRepository.getInstance().getMemberId(), 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 detachView() {
        mView = null;
    }

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

    private Activity getActivity() {
        return mActivity;
    }
}
