package kr.co.hanwha.hifive.screen.main.setting;

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.HelpdeskDataRoot;
import kr.co.hanwha.hifive.data.model.MemberInfoData;
import kr.co.hanwha.hifive.data.source.TaskCampaignDataRepository;
import kr.co.hanwha.hifive.data.source.TaskDataRepository;
import kr.co.hanwha.hifive.data.source.TaskMemberDataRepository;
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.main.setting.contract.HelpdeskContract;
import kr.co.hanwha.hifive.util.HFDateUtil;

public class HelpdeskPresenter implements HelpdeskContract.Presenter {
    private HelpdeskContract.View mView;
    private HelpdeskDataRoot mData;

    public HelpdeskPresenter(@NonNull HelpdeskContract.View view) {
        this.mView = view;
    }

    @Override
    public void detachView() {

    }

    @Override
    public void initData() {
        String dueTo = HFDateUtil.getDateHelpdesk();

        TaskDataRepository.getInstance().requestHelpdeskDetails(getContext(), new HanwhaNetworkCallback<HelpdeskDataRoot>() {
            @Override
            public void onSuccess(HelpdeskDataRoot response) {
                mData = response;
            }

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

                    @Override
                    public void onPositive() {
                        initData();
                    }
                });
            }

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

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

    @Override
    public void requestSendTask(@NonNull final String task_name) {

        if (TextUtils.isEmpty(task_name) || mData == null) return;

        String dueTo = HFDateUtil.getDateHelpdesk();

        ArrayList<MemberInfoData> listSelectedMember = mData.getHelpdeskMemberInfo();

        CampaignData campaignData = mData.getCampaignData();

        TaskDataRepository.getInstance().requestSendTask(getContext(),
                task_name,
                dueTo,
                listSelectedMember,
                campaignData,
                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 Context getContext() {
        return (Context) mView;
    }

    private Activity getActivity() {
        return (Activity) mView;
    }
}
