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.source.AlarmDataRepository;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;

/**
 * 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;

    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) {
        AlarmDataRepository.getInstance().getAlarmList(getContext(), new HanwhaNetworkCallback<ArrayList<AlarmData>>() {
            @Override
            public void onSuccess(ArrayList<AlarmData> response) {
                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.showLoadingDialog(false);
                mView.setRefreshing(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.getNoti_info_id(), new HanwhaNetworkCallback() {
            @Override
            public void onSuccess(Object response) {
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {

            }

            @Override
            public void onPreExecute() {

            }

            @Override
            public void onPostExecute() {

            }
        });
    }
}