package com.danding.yang800.Fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.danding.yang800.Adapter.TicketNotUseAdapter;
import com.danding.yang800.MyTicketActivity;
import com.danding.yang800.Presenter.MyTicketNotUseFragPresenter;
import com.danding.yang800.R;
import com.danding.yang800.View.XListView;

@SuppressLint({"ValidFragment", "NewApi"})
public class MyTicketNotUseFrag extends MyBaseFragment implements XListView.IXListViewListener {
    private MyTicketActivity context;
    XListView shop_frag_list;
    TicketNotUseAdapter adapter;
    MyTicketNotUseFragPresenter presenter;
    RelativeLayout shop_frag_emptyLayout;
    TextView shop_frag_emptyTv;
    int page = 1;

    public MyTicketNotUseFrag(MyTicketActivity context) {
        this.context = context;
    }

    int totalPage = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.shop_frag, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        initData();
        super.onActivityCreated(savedInstanceState);
    }

    private void initData() {
        shop_frag_list = (XListView) getView().findViewById(R.id.shop_frag_list);
        shop_frag_emptyLayout = (RelativeLayout) getView().findViewById(R.id.shop_frag_emptyLayout);
        shop_frag_emptyTv = (TextView) getView().findViewById(R.id.shop_frag_emptyTv);
        shop_frag_emptyTv.setText("暂无相关券");
        shop_frag_list.setPullRefreshEnable(true);
        shop_frag_list.setPullLoadEnable(true);
        shop_frag_list.setAutoLoadEnable(true);
        shop_frag_list.setXListViewListener(this);
        adapter = new TicketNotUseAdapter(context);
        presenter = new MyTicketNotUseFragPresenter(this, adapter);
        presenter.getInfo(page + "", false);
    }

    @Override
    public void onRefresh() {
        page = 1;
        onLoad();
        presenter.getInfo(page + "", false);
        context.reFresh();
    }

    @Override
    public void onLoadMore() {
        if (page < totalPage && adapter.getLists().size() > 0) {
            page++;
            presenter.getInfo(page + "", true);
        } else {
            onLoad();
        }
    }

    public void onLoad() {
        shop_frag_list.stopRefresh();
        shop_frag_list.stopLoadMore();
    }

    public void updateData(int total) {
        totalPage = total;
        if (page == 1) {
            shop_frag_list.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        if (adapter.getLists().size() == 0) {
            shop_frag_emptyLayout.setVisibility(View.VISIBLE);
            shop_frag_list.setVisibility(View.GONE);
        } else {
            shop_frag_emptyLayout.setVisibility(View.GONE);
            shop_frag_list.setVisibility(View.VISIBLE);
        }
        if (page < totalPage && adapter.getLists().size() > 0) {
            shop_frag_list.setPullLoadEnable(true);
        } else {
            shop_frag_list.setPullLoadEnable(false);
        }
        onLoad();
    }
}
