package kr.co.hanwha.hifive.screen.alarm;

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

import java.util.ArrayList;

import kr.co.hanwha.hifive.adapter.contract.AlarmListContract;
import kr.co.hanwha.hifive.data.model.AlarmData;
import kr.co.hanwha.hifive.data.model.AlarmListRoot;
import kr.co.hanwha.hifive.data.source.AlarmDataRepository;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;

/**
 * 알림 내역(목록) Presenter
 * <p>
 *
 * @see AlarmActivity
 * @see kr.co.hanwha.hifive.adapter.AlarmListAdapter
 * Created by sanghoonkwon on 2018. 1. 22..
 */

public class AlarmPresenter implements AlarmContract.Presenter {
    private AlarmContract.View mView;
    private AlarmListContract.View mAdapterView;
    private AlarmListContract.Model mAdapterModel;

    private int mCurrentPage = 1;
    private int mFianlPage = 0;
    private boolean isLoading = false;
    private ArrayList<AlarmData> mListData;

    public AlarmPresenter(@NonNull AlarmContract.View view,
                          @NonNull AlarmListContract.View adapterView,
                          @NonNull AlarmListContract.Model adapterModel) {
        mView = view;
        mAdapterView = adapterView;
        mAdapterModel = adapterModel;
    }

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

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


    @Override
    public void initData(final boolean needLoading) {
        if (mListData == null) mListData = new ArrayList<>();
        else mListData.clear();

        mCurrentPage = 1;

        loadData(needLoading);
    }

    @Override
    public void loadData(final boolean needLoading) {
        isLoading = true;

        AlarmDataRepository.getInstance().getAlarmList(getContext(), mCurrentPage, new HanwhaNetworkCallback<AlarmListRoot>() {
            @Override
            public void onSuccess(AlarmListRoot response) {
                if (mListData == null) mListData = new ArrayList<>();
                if (response != null) {
                    if (response.getListNotiHist() != null)
                        mListData.addAll(response.getListNotiHist());

                    mFianlPage = response.getFinalPageNo();
                }

                mAdapterModel.addItems(mListData);
                mAdapterModel.setVisibleFooter(isMoreData());
                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.showLoadingDialog(false);
                mView.setRefreshing(false);
                isLoading = false;
            }
        });
    }

    @Override
    public void loadMore() {
        if (isLoading || isMoreData() == false) {
            return;
        }

        mCurrentPage += 1;
        loadData(false);
    }

    @Override
    public void setNotificationOpen(@NonNull int index, @NonNull AlarmData data) {
        data.setIs_opened(true);
        mAdapterModel.replaceItem(index, data);
        mAdapterView.notifyDataAll();

        AlarmDataRepository.getInstance().requestNotificationOpen(getContext(), data, new HanwhaNetworkCallback() {
            @Override
            public void onSuccess(Object response) {
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {

            }

            @Override
            public void onPreExecute() {

            }

            @Override
            public void onPostExecute() {

            }
        });
    }

    private boolean isMoreData() {
        return mCurrentPage < mFianlPage;
    }
}