package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.sheep.gamegroup.di.components.DaggerWithdrawalListComponent;
import com.sheep.gamegroup.di.modules.WishdrawalListModule;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.WithdrawalEty;
import com.sheep.gamegroup.presenter.WithdrawalListContract;
import com.sheep.gamegroup.presenter.WithdrawalListPresenter;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.view.adapter.WithdrawalListAdp;
import com.sheep.gamegroup.view.customview.RefreshLayout;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

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

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 提现记录记录
 * Created by ljy on 2018/3/21.
 */

public class WithdrawalListAct extends BaseActivity implements WithdrawalListContract.View{
    @BindView(R.id.withdrawal_listview)
    ListView withdrawalListview;
    @BindView(R.id.swipe_container)
    RefreshLayout swipe_container;
    @BindView(R.id.empty_view)
    View empty_view;

    @Inject
    WithdrawalListPresenter presenter;

    private List<WithdrawalEty> etyList = new ArrayList<>();
    private WithdrawalListAdp adp;
    private Activity activity;
    private int page = 1;//页数
    private int per_page = DataUtil.PER_PAGE;

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

    @Override
    public void initView() {
        activity = this;
        DaggerWithdrawalListComponent.builder()
                .netComponent(SheepApp.getInstance().getNetComponent())
                .wishdrawalListModule(new WishdrawalListModule(this))
                .build()
                .inject(this);
        TitleBarUtils.getInstance()
                .setTitle(this, "提现明细")
                .setTitleFinish(this);
        swipe_container.setFooterView(this, withdrawalListview, R.layout.listview_footer);
        adp = new WithdrawalListAdp(activity, etyList);
        withdrawalListview.setAdapter(adp);
        adp.notifyDataSetChanged();

        swipe_container.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                etyList.clear();
                page = 1;
                initData();
            }
        });

        swipe_container.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                swipe_container.setLoading(false);
                if(etyList != null && etyList.size() >= per_page*page){
                    page += 1;
                    initData();
                }else {

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            swipe_container.setRefreshing(false);
                        }
                    },1000);
                }

            }
        });
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        empty_view.setVisibility(View.INVISIBLE);
        presenter.goWithdrawal(page, per_page, 0+"",System.currentTimeMillis()+"");

    }

    @Override
    public void updataView(Object o) {
        try{
            BaseMessage baseMessage = (BaseMessage) o;
            List<WithdrawalEty> releaseEtyList = JSONArray.parseArray(JSONArray.toJSONString(baseMessage.getData()), WithdrawalEty.class);
            for (WithdrawalEty ety: releaseEtyList){
                if(!etyList.contains(ety)){
                    etyList.add(ety);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        notifyDataSetChanged();
    }

    private void notifyDataSetChanged(){
        CommonUtil.getInstance().updateEmptyView(empty_view, etyList.isEmpty());
        hideProgress();
        swipe_container.setRefreshing(false);
        adp.notifyDataSetChanged();
    }
    @Override
    public void failView(Object o) {
        notifyDataSetChanged();
        G.showToast(((BaseMessage) o).getMsg()+"");
    }
}
