package com.bc_chat.im.activity.envelopes;

import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bc_chat.bc_base.config.RouteConfig;
import com.bc_chat.bc_base.contract.RedPacketRecordContract;
import com.bc_chat.bc_base.entity.wallet.RedPacketRecordEntity;
import com.bc_chat.bc_base.presenter.RedPacketRecrodPresenter;
import com.bc_chat.im.R;
import com.bc_chat.im.activity.envelopes.adapter.RedPacketRecordAdapter;
import com.zhaohaoting.framework.abs.BaseActivity;
import com.zhaohaoting.framework.view.recyclerview.BasicRecyclerAdapter;
import com.zhaohaoting.framework.view.recyclerview.CustomDividerItemDecoration;
import com.zhaohaoting.framework.view.recyclerview.CustomRecyclerView;
import com.zhaohaoting.framework.view.recyclerview.WrapContentLinearLayoutManager;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的钱包
 */
@Route(path = RouteConfig.REDPACKET_RECORD_ACTIVITY)
public class RedPacketRecordActivity extends BaseActivity<RedPacketRecrodPresenter> implements RedPacketRecordContract.View, SwipeRefreshLayout.OnRefreshListener {

    //列表
    public CustomRecyclerView listView;
    //通用的adapter
    public BasicRecyclerAdapter<RedPacketRecordEntity> basicAdapter;
    //下拉刷新
    public SwipeRefreshLayout swipeRefreshLayout;

    List<RedPacketRecordEntity> recordEntityArrayList = new ArrayList<>();

    @Override
    protected RedPacketRecrodPresenter initPresenter() {
        return new RedPacketRecrodPresenter<>(this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_redpacket_record;
    }

    @Override
    protected void initialize() {
        initView();
        initData();
    }

    private void initData() {
        getPresenter().myRedPacketLog(1, 1, 1000);
    }

    private void initListView() {
        listView = (CustomRecyclerView) findViewById(R.id.rv_list);
        listView.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayout.VERTICAL, false));
        listView.setNestedScrollingEnabled(false);

        CustomDividerItemDecoration divider = new CustomDividerItemDecoration(this, CustomDividerItemDecoration.VERTICAL);
        divider.setDrawable(getResources().getDrawable(R.drawable.recycler_line));
        listView.addItemDecoration(divider);

        //init swiperefreshlayout
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_refresh);
        swipeRefreshLayout.setColorSchemeColors(
                getResources().getColor(R.color.colorPrimaryDark), getResources().getColor(R.color.colorPrimary), getResources().getColor(R.color.colorAccent));
        swipeRefreshLayout.setOnRefreshListener(this);

        RedPacketRecordEntity redPacketRecordEntity = new RedPacketRecordEntity();
        redPacketRecordEntity.setId("1");
        redPacketRecordEntity.setAmount("10");
        redPacketRecordEntity.setMark("+");
        redPacketRecordEntity.setTime(System.currentTimeMillis() + "");

        basicAdapter = new RedPacketRecordAdapter(this, recordEntityArrayList);
        listView.setAdapter(basicAdapter);

    }

    private void initView() {
        initListView();
    }

    @Override
    public void myRedPacketLogSuccess(List<? extends RedPacketRecordEntity> dataList) {
        if (dataList != null) {
            recordEntityArrayList = (List<RedPacketRecordEntity>) dataList;
            basicAdapter.setDataCoverList(recordEntityArrayList);
            basicAdapter.notifyDataSetChanged();
            hideSwipeRefreshLayout();
        }
    }

    @Override
    public void myRedPacketLogFailed(@Nullable Exception exception) {
        hideSwipeRefreshLayout();
    }

    @Override
    public void onRefresh() {
        getPresenter().myRedPacketLog(1, 1, 1000);
    }


    /**
     * 隐藏下拉刷新按钮
     */
    public void hideSwipeRefreshLayout() {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
    }

}
