package cn.winxo.gank.module.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import cn.winxo.gank.R;
import cn.winxo.gank.adapter.DetailTitleBinder;
import cn.winxo.gank.adapter.DetailViewBinder;
import cn.winxo.gank.base.BaseMvpActivity;
import cn.winxo.gank.data.Injection;
import cn.winxo.gank.data.entity.constants.Constant;
import cn.winxo.gank.data.entity.remote.GankData;
import cn.winxo.gank.module.contract.DetailContract;
import cn.winxo.gank.module.presenter.DetailPresenter;
import cn.winxo.gank.util.Toasts;
import java.text.SimpleDateFormat;
import java.util.Locale;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class DetailActivity extends BaseMvpActivity<DetailContract.Presenter> implements DetailContract.View {

  protected Toolbar mToolbar;
  protected RecyclerView mRecycler;
  protected SwipeRefreshLayout mSwipeLayout;
  private long mDate;
  private MultiTypeAdapter mTypeAdapter;

  @Override protected int setLayoutResourceID() {
    return R.layout.activity_detail;
  }

  @Override protected void init(Bundle savedInstanceState) {
    super.init(savedInstanceState);
    mDate = getIntent().getLongExtra(Constant.ExtraKey.DATE, -1);
  }

  @Override protected void initView() {
    mToolbar = findViewById(R.id.toolbar);
    mRecycler = findViewById(R.id.recycler);
    mSwipeLayout = findViewById(R.id.swipe_layout);

    mToolbar.setNavigationOnClickListener(view -> finish());
    mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);

    mRecycler.setLayoutManager(new LinearLayoutManager(this));
    mTypeAdapter = new MultiTypeAdapter();
    mTypeAdapter.register(String.class, new DetailTitleBinder());
    DetailViewBinder detailViewBinder = new DetailViewBinder();
    detailViewBinder.setOnItemTouchListener((v, gankData) -> {
      Intent intent = new Intent();
      intent.setClass(DetailActivity.this, WebActivity.class);
      intent.putExtra("url", gankData.getUrl());
      intent.putExtra("name", gankData.getDesc());
      startActivity(intent);
    });
    mTypeAdapter.register(GankData.class, detailViewBinder);
    mRecycler.setAdapter(mTypeAdapter);
    mSwipeLayout.setOnRefreshListener(() -> mPresenter.refreshDayGank(mDate));
  }

  @Override protected void initData() {
    if (mDate != -1) {
      String dateShow = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(mDate);
      mToolbar.setTitle(dateShow);
      mSwipeLayout.setRefreshing(true);
      mPresenter.loadDayGank(mDate);
    } else {
      finish();
    }
  }

  @Override protected DetailPresenter onLoadPresenter() {
    return new DetailPresenter(this, Injection.provideGankDataSource(this));
  }

  @Override public void showLoading() {
    mSwipeLayout.setRefreshing(true);
  }

  @Override public void hideLoading() {
    mSwipeLayout.setRefreshing(false);
  }

  @Override public void loadSuccess(Items items) {
    hideLoading();
    mTypeAdapter.setItems(items);
    mTypeAdapter.notifyDataSetChanged();
  }

  @Override public void loadFail(String message) {
    Toasts.showShort("数据加载失败，请稍后再试");
    Log.e("DetailActivity", "loadFail: " + message);
  }
}
