package com.danding.yang800;

import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.danding.yang800.Adapter.MarketInfoAdapter;
import com.danding.yang800.Entity.MarketShopInfo;
import com.danding.yang800.Presenter.MarketPlatFormInfoActivityPresenter;
import com.danding.yang800.View.XListView;
import com.yanzhenjie.sofia.Sofia;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MarketPlatFormInfoActivity extends BaseActivity implements XListView.IXListViewListener {
    @BindView(R.id.marketPlatInfo_list)
    XListView marketPlatInfo_list;
    MarketInfoAdapter adapter;
    MarketPlatFormInfoActivityPresenter presenter;
    @BindView(R.id.platInfo_ticketNameTv)
    TextView platInfo_ticketNameTv;
    @BindView(R.id.platInfo_ticketFeeTv)
    TextView platInfo_ticketFeeTv;
    @BindView(R.id.platInfo_getTicketTv)
    TextView platInfo_getTicketTv;
    @BindView(R.id.platInfo_useTicketTv)
    TextView platInfo_useTicketTv;
    @BindView(R.id.platInfo_backTv)
    TextView platInfo_backTv;
    int page = 1;
    int totalPage = 1;
    int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_market_plat_form_info);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        ButterKnife.bind(this);
        marketPlatInfo_list.setPullRefreshEnable(true);
        marketPlatInfo_list.setPullLoadEnable(false);
        marketPlatInfo_list.setAutoLoadEnable(false);
        marketPlatInfo_list.setXListViewListener(this);
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 999);
        if (savedInstanceState != null) {
            id = savedInstanceState.getInt("id");
        }
        adapter = new MarketInfoAdapter(this);
        presenter = new MarketPlatFormInfoActivityPresenter(this, adapter);
        presenter.getInfo(id + "", sp.getString("storeId", ""), page + "");
    }

    @OnClick({R.id.platInfo_backTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.platInfo_backTv:
                finish();
                break;
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        outState.putInt("id", id);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        id = savedInstanceState.getInt("id");
    }

    @Override
    public void onRefresh() {
        page = 1;
        onLoad();
        presenter.getInfo(id + "", sp.getString("storeId", ""), page + "");
    }

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

    public void initData(String name, String conditionFee, String receivedQuantity, String usedQuantity) {
        platInfo_ticketNameTv.setText(name);
        platInfo_ticketFeeTv.setText("金额：" + conditionFee + "元");
        platInfo_getTicketTv.setText("已领取：" + receivedQuantity + "张，");
        platInfo_useTicketTv.setText("已使用：" + usedQuantity + "张");
    }

    public void updateData(int total) {
        totalPage = total;
        if (page < total) {
            marketPlatInfo_list.setPullLoadEnable(true);
        } else {
            marketPlatInfo_list.setPullLoadEnable(false);
        }
        if (page == 1) {
            marketPlatInfo_list.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        onLoad();
    }

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