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

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.AlarmListAdapter;
import kr.co.hanwha.hifive.adapter.contract.AlarmListContract;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.model.AlarmData;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.util.HFInfoUtil;
import kr.co.hanwha.hifive.util.HFNotiUtil;

/**
 * 알림 내역(목록) View
 * <p>
 *
 * @see AlarmPresenter
 * Created by sanghoonkwon on 2018. 1. 22..
 */

public class AlarmActivity extends BaseActivity implements AlarmContract.View {
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;

    private AlarmListAdapter mAdapter;

    private AlarmPresenter mPresenter;

    @Override
    protected int getContentViewResource() {
        return R.layout.activity_alarm;
    }

    @Override
    protected BaseToolbarType isToolbarStyle() {
        return BaseToolbarType.ONLY_BACK;
    }

    @Override
    protected BaseAnimationType isShowActivityAnimation() {
        return BaseAnimationType.RIGHT;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle(getString(R.string.alarm_title));

        mAdapter = new AlarmListAdapter(this, onAlarmListListener);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(onRefreshListener);

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addOnScrollListener(onScrollListener);

        mPresenter = new AlarmPresenter(this, mAdapter, mAdapter);
        mPresenter.initData(true);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (HFInfoUtil.getNeedRefreshAlarmList(this)) {
            mPresenter.initData(true);
        }
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        mSwipeRefreshLayout.setRefreshing(refreshing);
    }

    @Override
    public void refreshData() {
        mPresenter.initData(false);
    }


    /**************************************************
     * AlarmListContract.OnAlarmListListener
     **************************************************/
    AlarmListContract.OnAlarmListListener onAlarmListListener = new AlarmListContract.OnAlarmListListener() {
        @Override
        public void onItemClicked(@NonNull int index, @NonNull AlarmData data) {
            mPresenter.setNotificationOpen(index, data);
            HFNotiUtil.pendingNotification(AlarmActivity.this, data);
            HFInfoUtil.setNeedRefreshAlarmList(AlarmActivity.this);
        }
    };

    /**************************************************
     * SwipeRefreshLayout.OnRefreshListener
     **************************************************/
    SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPresenter.initData(false);
        }
    };
    /**************************************************
     * RecyclerView.OnScrollListener
     **************************************************/
    RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            int firstVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();

            int visibleItemCount = recyclerView.getChildCount();
            int totalItemCount = recyclerView.getLayoutManager().getItemCount();
            boolean isLastCellVisible = ((totalItemCount <= firstVisibleItem + visibleItemCount) ? true : false);
            if (isLastCellVisible) {
                mPresenter.loadMore();
            }
        }
    };
}