package com.tmac.zhdaily.view.impl;

import android.os.Bundle;
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.view.View;

import com.tmac.zhdaily.R;
import com.tmac.zhdaily.adapter.HomeAdapter;
import com.tmac.zhdaily.base.BaseFragment;
import com.tmac.zhdaily.enums.Events;
import com.tmac.zhdaily.enums.OnItemClickListener;
import com.tmac.zhdaily.enums.RxBus;
import com.tmac.zhdaily.model.LatestArticle;
import com.tmac.zhdaily.model.StoriesEntity;
import com.tmac.zhdaily.presenter.HomePresenter;
import com.tmac.zhdaily.utils.UINavi;
import com.tmac.zhdaily.utils.ViewUtils;
import com.tmac.zhdaily.view.HomeView;
import com.tmac.zhdaily.widget.AutoLoadMoreRecyclerView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.functions.Action1;
import rx.subscriptions.CompositeSubscription;

public class HomeFragment extends BaseFragment<HomeView, HomePresenter> implements HomeView, AutoLoadMoreRecyclerView.OnLoadMoreListener {

    private CompositeSubscription mCompositeSubscription = new CompositeSubscription();

    @Bind(R.id.recycler_view)
    AutoLoadMoreRecyclerView mRecyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout mRefreshLayout;

    private HomeAdapter mHomeAdapter;

    public HomeFragment() {
    }

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected HomePresenter createPresenter() {
        return new HomePresenter();
    }

    @Override
    protected HomeView createView() {
        return this;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerEvent();
    }

    private void registerEvent() {
        mCompositeSubscription.add(
                RxBus.getDefault()
                        .toObserverable(Events.OnBannerClickEvent.class)
                        .subscribe(new Action1<Events.OnBannerClickEvent>() {
                            @Override
                            public void call(Events.OnBannerClickEvent event) {
                                UINavi.toDetailActivity(getActivity(), event.getPointer(),
                                        event.getId(), event.getTitle());
                            }
                        })
        );
    }

    @Override
    protected void initView(View view) {
        ButterKnife.bind(this, view);
        setupRefresh();
        setupRecyclerView();
    }

    private void setupRefresh() {
        mRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light
        );

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.loadLatest();
            }
        });
    }

    private void setupRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mHomeAdapter = new HomeAdapter(mContext);
        mRecyclerView.setAdapter(mHomeAdapter);
        mHomeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int id, String title) {
                int[] startingLocation = ViewUtils.getViewLocation(view);
                UINavi.toDetailActivity(getActivity(), startingLocation, id, title);
            }
        });

        mRecyclerView.setOnLoadMoreListener(this);
    }

    @Override
    protected void initData() {
        mPresenter.loadLatest();
    }

    @Override
    public void updateBannerData(List<LatestArticle.TopStoriesEntity> topStories) {
        mHomeAdapter.updateBannerData(topStories);
    }

    @Override
    public void updateList(List<StoriesEntity> entities) {
        mRecyclerView.onLoadFinish();
        mRefreshLayout.setRefreshing(false);

        mHomeAdapter.replaceData(entities);
    }

    @Override
    public void onLoadMore() {
        mPresenter.loadMore();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }
}
