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

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.MainProjectListContract;
import kr.co.hanwha.hifive.data.model.MainProjectRoot;
import kr.co.hanwha.hifive.data.model.ProjectInfo;
import kr.co.hanwha.hifive.data.model.ProjectInfoData;
import kr.co.hanwha.hifive.data.model.SpinnerData;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.data.source.ProjectDataRepository;
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.util.HFActivityUtil;

/**
 * Unit View Presenter
 *
 * @see MainProjectFragment
 * Created by sanghoonkwon on 2018. 2. 20..
 */

public class MainProjectPresenter implements MainProjectContract.Presenter {
    private Context mContext;
    private MainProjectContract.View mView;
    private MainProjectListContract.View mAdapterView;
    private MainProjectListContract.Model mAdapterModel;

    private ProjectInfo mProjectInfo;
    private int mProject_id = 0;
    private boolean mIsAll = false;
    private ArrayList<SpinnerData> mListProjects;

    public MainProjectPresenter(@NonNull Context context,
                                @NonNull MainProjectContract.View view,
                                @NonNull MainProjectListContract.View adapterView,
                                @NonNull MainProjectListContract.Model adapterModel) {
        mContext = context;
        mView = view;
        mAdapterView = adapterView;
        mAdapterModel = adapterModel;
    }

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

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

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

    @Override
    public void setProjectId(int projectId) {
        if (mProject_id != projectId) {
            mProject_id = projectId;

            mAdapterModel.setProjectId(mProject_id);
            mAdapterView.notifyDataAll();

            initTaskData(true);
        }
    }

    @Override
    public int getProjectId() {
        return mProject_id;
    }

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

    @Override
    public void initProjectList() {
        if (mListProjects == null) mListProjects = new ArrayList<>();
        else mListProjects.clear();

        ProjectDataRepository.getInstance().requestProjectList(getContext(), new HanwhaNetworkCallback<ArrayList<ProjectInfoData>>() {
            @Override
            public void onSuccess(ArrayList<ProjectInfoData> response) {
                if (response != null && response.size() > 0) {

                    for (int i = 0; i < response.size(); i++) {
                        ProjectInfoData data = response.get(i);
                        mListProjects.add(new SpinnerData(Integer.toString(i), data));
                    }

                    mAdapterModel.addItems(mListProjects);
                    mAdapterView.notifyDataAll();

                    // SElECT DEFAULT PROJECT...
                    int project_id = response.get(0).getProject_id();
                    if (mProject_id == 0)
                        setProjectId(project_id);

                    mView.setEmptyView(false);
                } else {
                    mView.setEmptyView(true);
                    mView.showLoadingDialog(false);
                }
            }

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

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

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

    @Override
    public void initTaskData(@NonNull final boolean needLoading) {
        if (mProject_id == 0) {
            mView.setRefreshing(false);
            mView.showLoadingDialog(false);
            return;
        }

        ProjectDataRepository.getInstance().requestTaskListByCampaign(getContext(), mProject_id, mIsAll, new HanwhaNetworkCallback<MainProjectRoot>() {
            @Override
            public void onSuccess(MainProjectRoot response) {
                mView.setBackgroundView(false);

                if (response != null) {
                    mProjectInfo = response.getProjectInfo();
                    mAdapterModel.addItems(response);
                }
                mAdapterView.notifyDataAll();
            }

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

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

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

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

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

    @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) {
        ProjectDataRepository.getInstance().requestUpdateProjectColor(getContext(), mProject_id, type, new HanwhaNetworkCallback() {
            @Override
            public void onSuccess(Object response) {
                initTaskData(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);
            }
        });
    }
}
