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

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

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.contract.MainProjectListContract;
import kr.co.hanwha.hifive.data.model.MainProjectRoot;
import kr.co.hanwha.hifive.data.model.ProjectInfo;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.data.source.ProjectDataRepository;
import kr.co.hanwha.hifive.dialog.HFDefaultDialog;
import kr.co.hanwha.hifive.dialog.HFProjectColorDialog;
import kr.co.hanwha.hifive.enums.ProjectEvaluationType;
import kr.co.hanwha.hifive.network.HFNetworkUtil;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.screen.main.dashboard.contract.DashboardProjectContract;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;

/**
 * Created by sanghoonkwon on 2018. 3. 19..
 */

public class DashboardProjectPresenter implements DashboardProjectContract.Presenter {
    private DashboardProjectContract.View mView;
    private MainProjectListContract.View mAdapterView;
    private MainProjectListContract.Model mAdapterModel;

    private ProjectInfo mProjectInfo;
    private boolean mIsAll = false;

    public DashboardProjectPresenter(@NonNull DashboardProjectContract.View view,
                                     @NonNull MainProjectListContract.View adapterView,
                                     @NonNull MainProjectListContract.Model adapterModel) {
        mView = view;
        mAdapterView = adapterView;
        mAdapterModel = adapterModel;
    }

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

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

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

    @Override
    public void setIsAll(boolean isAll) {
        if (mIsAll == isAll) return;
        mIsAll = isAll;
        initTaskData(mProjectInfo.getProject_id(), true);
    }

    @Override
    public void initTaskData(@NonNull final int projectId, @NonNull final boolean needLoading) {
        ProjectDataRepository.getInstance().requestTaskListByCampaign(getContext(), projectId, mIsAll, new HanwhaNetworkCallback<MainProjectRoot>() {
            @Override
            public void onSuccess(MainProjectRoot response) {
                if (response != null) {
                    mProjectInfo = response.getProjectInfo();
                    mAdapterModel.addItems(response);
                }
                mAdapterView.notifyDataAll();
            }

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

                    @Override
                    public void onPositive() {
                        initTaskData(projectId, needLoading);
                    }
                });
            }

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

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

    @Override
    public void showProjectColorDialog() {
        if (mProjectInfo == null) return;
        new HFProjectColorDialog(getActivity(),
                mProjectInfo.getProject_name(),
                mProjectInfo.getEvaluation(),
                new HFProjectColorDialog.OnDialogCallback() {
                    @Override
                    public void onConfirm(ProjectEvaluationType type) {
                        updateProjectColor(type);
                    }
                }).show();
    }

    @Override
    public void updateProjectColor(ProjectEvaluationType type) {
        if (mProjectInfo == null) return;
        ProjectDataRepository.getInstance().requestUpdateProjectColor(getContext(), mProjectInfo.getProject_id(), type, new HanwhaNetworkCallback() {
            @Override
            public void onSuccess(Object response) {
                HFInfoUtil.setNeedRefreshMainProjectList(getActivity());
                initTaskData(mProjectInfo.getProject_id(), true);
            }

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

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

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

    @Override
    public void showEditIssueDialog() {
        if (mProjectInfo == null) return;

        HFInfoUtil.setNeedRefreshMainProjectList(getActivity());

        HFNetworkUtil networkUtil = new HFNetworkUtil();
        String encryption = MemberDataRepository.getInstance().getEncryptionMemberId();
        String url = networkUtil.getWebUrlSettingEditUnit();
        HFActivityUtil.startIssueEditActivity(getActivity(),
                url,
                encryption,
                mProjectInfo.getProject_id());
    }
}
