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.WithdrawRecordContract;
import com.bc_chat.bc_base.entity.wallet.RedPacketRecordEntity;
import com.bc_chat.bc_base.entity.wallet.WithdrawRecordBean;
import com.bc_chat.bc_base.presenter.WithdrawRecordPresenter;
import com.bc_chat.im.R;
import com.bc_chat.im.activity.envelopes.adapter.WithdrawalRecordAdapter;
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.WITHDRAWAL_RECORD_ACTIVITY)
public class WithdrawalRecordActivity extends BaseActivity<WithdrawRecordPresenter> implements WithdrawRecordContract.View, SwipeRefreshLayout.OnRefreshListener {

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

    List<WithdrawRecordBean> withdrawRecordBeanList = new ArrayList<>();

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

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

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

    private void initData() {
        getPresenter().getWithdrawRecord(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);

        basicAdapter = new WithdrawalRecordAdapter(this, withdrawRecordBeanList);
        listView.setAdapter(basicAdapter);

    }

    private void initView() {
        initListView();
    }


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

    @Override
    public void getWithdrawRecordSuccess(@Nullable List<? extends WithdrawRecordBean> dataList) {
        if (dataList != null) {
            withdrawRecordBeanList = (List<WithdrawRecordBean>) dataList;
            basicAdapter.setDataCoverList(withdrawRecordBeanList);
            basicAdapter.notifyDataSetChanged();
            hideSwipeRefreshLayout();
        }
    }

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

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