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.adapter.contract.MainDashboardListContract;
import kr.co.hanwha.hifive.data.model.DabDataRoot;
import kr.co.hanwha.hifive.data.source.ProjectDataRepository;
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.MainDashboardContract;

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

public class MainDashboardPresenter implements MainDashboardContract.Presenter {
    private Context mContext;
    private Activity mActivity;
    private MainDashboardContract.View mView;
    private MainDashboardListContract.View mAdapterViewGreen;
    private MainDashboardListContract.Model mAdapterModelGreen;
    private MainDashboardListContract.View mAdapterViewOrange;
    private MainDashboardListContract.Model mAdapterModelOrange;


    public MainDashboardPresenter(@NonNull Context context,
                                  @NonNull MainDashboardContract.View view,
                                  @NonNull MainDashboardListContract.View adapterViewGreen,
                                  @NonNull MainDashboardListContract.Model adapterModelGreen,
                                  @NonNull MainDashboardListContract.View adapterViewOrange,
                                  @NonNull MainDashboardListContract.Model adapterModelOrange) {
        mContext = context;
        mActivity = (Activity) context;
        mView = view;
        mAdapterViewGreen = adapterViewGreen;
        mAdapterModelGreen = adapterModelGreen;
        mAdapterViewOrange = adapterViewOrange;
        mAdapterModelOrange = adapterModelOrange;
    }

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

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

    @Override
    public void initData(final boolean needLoading) {
        ProjectDataRepository.getInstance().requestDashboardList(getContext(), new HanwhaNetworkCallback<DabDataRoot>() {
            @Override
            public void onSuccess(DabDataRoot response) {
                int orangeCount = 0;
                int greenCount = 0;
                if (response != null) {
                    mAdapterModelGreen.addItems(response.getTreeGreenProjectsMembers());
                    mAdapterModelOrange.addItems(response.getTreeOrangeProjectsMembers());

                    greenCount = response.getTreeGreenProjectsMembers() != null ? response.getTreeGreenProjectsMembers().size() : 0;
                    orangeCount = response.getTreeOrangeProjectsMembers() != null ? response.getTreeOrangeProjectsMembers().size() : 0;
                }

                mView.setEmptyViewGreen(greenCount == 0);
                mView.setEmptyViewOrange(orangeCount == 0);

                mAdapterViewGreen.notifyDataAll();
                mAdapterViewOrange.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);
            }
        });
    }
}
