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 java.util.Date;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.data.model.MemberInfoData;
import kr.co.hanwha.hifive.data.model.TaskData;
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.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;

    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() {
        TaskData taskData = TaskDataRepository.getTaskData();
        if (taskData != null) {
            mDueTo = HFStringUtil.getDisplayDueDateAddTask(taskData.getDue_to());
            mView.setTaskData(taskData);
            mView.setDuedate(mDueTo);

            ArrayList<MemberInfoData> list = new ArrayList<>();
            list.add(new MemberInfoData(taskData.getMember_id(),
                    taskData.getMember_name(),
                    taskData.getMember_profile_url(),
                    taskData.getProject_id(),
                    taskData.getProject_name()));
            mView.setReceiverList(list, false);
        } else getActivity().finish();
    }

    @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 isMyTask() {
        ArrayList<MemberInfoData> list = TaskDataRepository.getSelectedDataList();
        if (list != null && list.size() == 1) {
            return (list.get(0).getMember_id() == MemberDataRepository.getInstance().getMemberId());
        }
        return false;
    }

    @Override
    public boolean isRequest() {
        ArrayList<MemberInfoData> list = TaskDataRepository.getSelectedDataList();
        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.getSelectedDataList();

        if (TextUtils.isEmpty(task_name) || listSelected.isEmpty()) return;

        TaskDataRepository.getInstance().requestSendTask(getContext(), task_name,
                mDueTo, listSelected, 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) {
        if (TextUtils.isEmpty(task_name)) return;

        TaskData taskData = TaskDataRepository.getTaskData();
        taskData.setTask_name(task_name);
        taskData.setDue_to(mDueTo);
        TaskDataRepository.getInstance().requestUpdateTask(getContext(), taskData, 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() {
                        updateTask(task_name);
                    }
                });
            }

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

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

    }

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