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

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

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
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.model.TaskData;
import kr.co.hanwha.hifive.data.model.TaskDetailData;
import kr.co.hanwha.hifive.data.model.TaskDetailRoot;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.data.source.TaskDataRepository;
import kr.co.hanwha.hifive.dialog.HFCalendarDialog;
import kr.co.hanwha.hifive.dialog.HFDefaultDialog;
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.AddTaskContract;
import kr.co.hanwha.hifive.util.HFDateUtil;
import kr.co.hanwha.hifive.util.HFStringUtil;

/**
 * Add Task : Presenter
 *
 * @see AddTaskActivity
 * Created by sanghoonkwon on 2018. 1. 18..
 */

public class AddTaskPresenter implements AddTaskContract.Presenter {
    private AddTaskContract.View mView;
    /**
     * 기한
     */
    private String mDueTo;
    private TaskDetailData mTaskDetailData;

    public AddTaskPresenter(@NonNull AddTaskContract.View view) {
        mView = view;
        mDueTo = HFDateUtil.getDateFormatToday(HFDateUtil.FORMAT_TASK_ADD_DUEDATE_SERVER);
    }

    @Override
    public void detachView() {
        mView = null;
        TaskDataRepository.getInstance().clearData();
        TaskDataRepository.destroyInstance();
    }

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

    @Override
    public void initData() {
        mDueTo = HFDateUtil.getDateFormatToday(HFDateUtil.FORMAT_TASK_ADD_DUEDATE_SERVER);
        mView.setDuedate(mDueTo);

        TaskDataRepository.getInstance().requestAllMember(getContext(), new HanwhaNetworkCallback<ArrayList<MemberInfoData>>() {
            @Override
            public void onSuccess(ArrayList<MemberInfoData> response) {
            }

            @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 initEditData(@NonNull int task_id, @NonNull int project_id) {
        TaskDataRepository.getInstance().requestGetTaskDetail(getContext(), task_id, project_id, new HanwhaNetworkCallback<TaskDetailRoot>() {
            @Override
            public void onSuccess(TaskDetailRoot response) {
                if (response != null && response.getTaskResult() != null) {
                    mTaskDetailData = response.getTaskResult();
                    mDueTo = HFStringUtil.getDisplayDueDateAddTask(mTaskDetailData.getDue_to());
                    mView.setTaskData(mTaskDetailData);
                    mView.setDuedate(mDueTo);

                    ArrayList<MemberInfoData> list = new ArrayList<>();
                    MemberInfoData member = new MemberInfoData(mTaskDetailData.getMember_id(),
                            mTaskDetailData.getMember_name(),
                            mTaskDetailData.getMember_profile_url(),
                            mTaskDetailData.getProject_id(),
                            mTaskDetailData.getProject_name());
                    list.add(member);
                    TaskDataRepository.setMemberInfoData(member);//Campaign 정보 조회를 위함..-_- 수정 필요
                    mView.setReceiverList(list, false);


                    if (response.getListCampaigns() != null && response.getListCampaigns().size() > 0) {
                        CampaignData campaign = response.getListCampaigns().get(0);
                        TaskDataRepository.setCampaignListSelect(campaign);
                        TaskDataRepository.setVerifyCampaignInfo(campaign.getKey());
                        mView.setCampaignInfo(campaign);

                    }
                } else getActivity().finish();
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                mView.showAlert(error, new HFSingleDialog.OnDialogCallback() {
                    @Override
                    public void onPositive() {
                        getActivity().finish();
                    }
                });
            }

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

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

    @Override
    public void showCalendarDialog() {
        new HFCalendarDialog(getContext(), mDueTo, new HFCalendarDialog.OnDialogCallback() {
            @Override
            public void onConfirm(@NonNull String selectedDate) {
                mDueTo = selectedDate;
                mView.setDuedate(selectedDate);
            }
        }).show();
    }

    @Override
    public boolean isRequest() {
        ArrayList<MemberInfoData> list = TaskDataRepository.getSelectedMemberList();
        if (list != null) {
            if (list.size() == 1 && list.get(0).getMember_id() != MemberDataRepository.getInstance().getMemberId())
                return true;
            else if (list.size() > 1) return true;

        }
        return false;
    }

    @Override
    public void requestSendTask(@NonNull final String task_name) {
        ArrayList<MemberInfoData> listSelected = TaskDataRepository.getSelectedMemberList();

        if (TextUtils.isEmpty(task_name)) return;

        TaskDataRepository.getInstance().requestSendTask(getContext(),
                task_name,
                mDueTo,
                listSelected,
                TaskDataRepository.getSelectedCampaignData(),
                new HanwhaNetworkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        mView.showToastConfirm(R.string.alert_completed);
                        getActivity().setResult(Activity.RESULT_OK);
                        getActivity().finish();
                    }

                    @Override
                    public void onFailure(HanwhaNetworkError error) {
                        mView.showAlert(error, new HFDefaultDialog.OnDialogCallback() {
                            @Override
                            public void onNegative() {

                            }

                            @Override
                            public void onPositive() {
                                requestSendTask(task_name);
                            }
                        });
                    }

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

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

    @Override
    public void updateTask(@NonNull final String task_name, @NonNull final boolean isReRequest) {
        if (TextUtils.isEmpty(task_name)) return;

        mTaskDetailData.setTask_name(task_name);
        mTaskDetailData.setDescription(task_name);
        mTaskDetailData.setDue_to(mDueTo);
        TaskDataRepository.getInstance().requestUpdateTask(getContext(), mTaskDetailData,
                TaskDataRepository.getSelectedCampaignData(),
                new HanwhaNetworkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        if (isReRequest) {
                            doReRequest(mTaskDetailData);
                        } else {
                            mView.showToastConfirm(R.string.alert_completed);
                            getActivity().setResult(Activity.RESULT_OK);
                            getActivity().finish();
                        }
                    }

                    @Override
                    public void onFailure(HanwhaNetworkError error) {
                        mView.showAlert(error, new HFDefaultDialog.OnDialogCallback() {
                            @Override
                            public void onNegative() {

                            }

                            @Override
                            public void onPositive() {
                                updateTask(task_name, isReRequest);
                            }
                        });
                    }

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

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

    @Override
    public void doReRequest(@NonNull final TaskData taskData) {
        TaskDataRepository.getInstance().requestSetTaskReRequest(getContext(), taskData,
                new HanwhaNetworkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        getActivity().setResult(Activity.RESULT_OK);
                        getActivity().finish();
                    }

                    @Override
                    public void onFailure(HanwhaNetworkError error) {
                        mView.showAlert(error, new HFDefaultDialog.OnDialogCallback() {
                            @Override
                            public void onNegative() {

                            }

                            @Override
                            public void onPositive() {
                                doReRequest(taskData);
                            }
                        });
                    }

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

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

    /**************************************************
     * Support methods
     **************************************************/
    private Activity getActivity() {
        return (Activity) mView;
    }
}
