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

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

import java.util.ArrayList;

import kr.co.hanwha.hifive.adapter.contract.MainHomeRequestListContract;
import kr.co.hanwha.hifive.adapter.contract.SettingMyHomeOkrListContract;
import kr.co.hanwha.hifive.adapter.contract.SettingMyHomeTodoListContract;
import kr.co.hanwha.hifive.data.model.CampaignTreeData;
import kr.co.hanwha.hifive.data.model.MainHomeAssignedRoot;
import kr.co.hanwha.hifive.data.model.MainHomeRequestRoot;
import kr.co.hanwha.hifive.data.model.ProfileData;
import kr.co.hanwha.hifive.data.model.ProfileRoot;
import kr.co.hanwha.hifive.data.model.TaskListData;
import kr.co.hanwha.hifive.data.source.MyHomeDataRepository;
import kr.co.hanwha.hifive.data.source.TaskCampaignDataRepository;
import kr.co.hanwha.hifive.data.source.TaskListDataRepository;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.screen.main.setting.myhome.contract.MyHomeContract;

/**
 * Created by aaron on 2018. 8. 28.
 */
class MyHomePresenter implements MyHomeContract.Presenter {
    private Context mContext;
    private MyHomeContract.View mView;
    private SettingMyHomeTodoListContract.View mAdapterViewTodo;
    private SettingMyHomeTodoListContract.Model mAdapterModelTodo;
    private SettingMyHomeOkrListContract.Model mAdapterModelOkr;
    private SettingMyHomeOkrListContract.View mAdpaterViewOkr;
    private MainHomeRequestListContract.View mAdapterViewRequest;
    private MainHomeRequestListContract.Model mAdapterModelRequest;

    public MyHomePresenter(@NonNull MyHomeContract.View view,
                           @NonNull SettingMyHomeTodoListContract.View adapterViewTodo,
                           @NonNull SettingMyHomeTodoListContract.Model adapterModelTodo,
                           @NonNull SettingMyHomeOkrListContract.View adapterViewOkr,
                           @NonNull SettingMyHomeOkrListContract.Model adapterModelOkr,
                           @NonNull MainHomeRequestListContract.View adapterViewRequest,
                           @NonNull MainHomeRequestListContract.Model adapterModelRequest) {
        this.mContext = (Context) view;
        this.mView = view;
        this.mAdapterViewTodo = adapterViewTodo;
        this.mAdapterModelTodo = adapterModelTodo;
        this.mAdpaterViewOkr = adapterViewOkr;
        this.mAdapterModelOkr = adapterModelOkr;
        this.mAdapterViewRequest = adapterViewRequest;
        this.mAdapterModelRequest = adapterModelRequest;
    }

    @Override
    public void refreshData() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initDataOkr(int selectMemberId, @NonNull final boolean needLoading) {
        ArrayList<Integer> listMemberInfo = new ArrayList<>();
        if (selectMemberId != 0)
            listMemberInfo.add(selectMemberId);

        TaskCampaignDataRepository.getInstance().requestCampaignTreeByMemberId(getContext(), listMemberInfo, new HanwhaNetworkCallback<ArrayList<CampaignTreeData>>() {
            @Override
            public void onSuccess(ArrayList<CampaignTreeData> response) {
                if (response != null) {
                    mAdapterModelOkr.addItems(response);
                }
                mAdpaterViewOkr.notifyDataAll();
            }

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

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

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

    @Override
    public void initDataTodo(int selectMemberId, @NonNull final boolean needLoading) {
        TaskListDataRepository.getInstance().requestTasksGetMainAssigned(getContext(), selectMemberId, new HanwhaNetworkCallback<MainHomeAssignedRoot>() {
            @Override
            public void onSuccess(MainHomeAssignedRoot response) {

                if (response != null) {
                    ArrayList<TaskListData> listOfToday = response.getListTodayAssignedTasks();
                    ArrayList<TaskListData> listOfTasks = new ArrayList<>();
                    ArrayList<TaskListData> listOfClosed = response.getListClosedTasks();
                    listOfTasks.addAll(response.getListWaitingTasks());
                    listOfTasks.addAll(response.getListNotTodayAssignedTasks());
                    mAdapterModelTodo.addItems(listOfToday, listOfTasks, listOfClosed);
                }
                mAdapterViewTodo.notifyDataAll();
            }

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

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

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

    @Override
    public void initDataRequest(int selectMemberId, @NonNull final boolean needLoading) {
        TaskListDataRepository.getInstance().requestTasksGetMainRequest(getContext(), selectMemberId, new HanwhaNetworkCallback<MainHomeRequestRoot>() {
            @Override
            public void onSuccess(MainHomeRequestRoot response) {

                if (response != null) {
                    ArrayList<TaskListData> listOfEvaluation = response.getListResolvedRequestTasks();
                    ArrayList<TaskListData> listOfRequested = new ArrayList<>();
                    listOfRequested.addAll(response.getListRequestTasks());
                    listOfRequested.addAll(response.getListRejectedRequestTasks());
                    mAdapterModelRequest.addItems(listOfEvaluation, listOfRequested);
                }
                mAdapterViewRequest.notifyDataAll();
            }

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

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

            @Override
            public void onPostExecute() {
//                isInitRequest = true;
                mView.stopRefreshRequest();
                mView.showLoadingDialog(false);
            }
        });
    }

    @Override
    public void initDataProfile(int selectMemberId, @NonNull final boolean needLoading) {
        MyHomeDataRepository.getInstance().requestMyHomeGetProfile(getContext(), selectMemberId, new HanwhaNetworkCallback<ProfileRoot>() {
            @Override
            public void onSuccess(ProfileRoot response) {
                ProfileData data = response.getMemberProfile();
                mView.setProfile(data);
                mView.setProfileUnit(data.getListMemberProjects());
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {

            }

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

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

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

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