package com.danding.yang800;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import com.danding.yang800.Adapter.WaitProfitAdapter;
import com.danding.yang800.Presenter.WaitProfitActivityPresenter;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.View.XListView;
import com.yanzhenjie.sofia.Sofia;

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

public class WaitProfitActivity extends BaseActivity implements XListView.IXListViewListener {
    @BindView(R.id.waitProfit_list)
    XListView waitProfit_list;
    @BindView(R.id.waitProfit_backTv)
    TextView waitProfit_backTv;
    WaitProfitActivityPresenter presenter;
    WaitProfitAdapter adapter;
    String storeId = "";
    private int Count = 0;
    int page=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wait_profit);
//        int type = StatusColorUtil.StatusBarLightMode(this);
//        if (type > 0) {
//            StatusColorUtil.setStatusBarColor(this, R.color.white);
//            StatusColorUtil.StatusBarLightMode(this, type);
//        }
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        ButterKnife.bind(this);
        initView();
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        storeId = sp.getString("storeId", "");
        adapter = new WaitProfitAdapter(this);
        presenter = new WaitProfitActivityPresenter(this, adapter);
        presenter.getOrderInfo( storeId, page + "", false);
    }
    android.widget.AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            Intent intent = new Intent(WaitProfitActivity.this, OrderDetailActivity.class);
            intent.putExtra("orderSn", adapter.getItem(position - 1).getOrderSn());
            intent.putExtra("packageSn", adapter.getItem(position - 1).getPackageSn());
            intent.putExtra("status", adapter.getItem(position - 1).getStatus());
            startActivityForResult(intent, 0);
        }
    };
    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

    private void initView() {
        waitProfit_list.setPullRefreshEnable(true);
        waitProfit_list.setPullLoadEnable(false);
        waitProfit_list.setAutoLoadEnable(true);
        waitProfit_list.setXListViewListener(this);
        waitProfit_list.setOnItemClickListener(listener);
    }

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


        }
    }

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

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


    public void updateAdapter(int totalCount) {
        Count = totalCount;
        if (adapter.getLists().size() > 0 && adapter.getLists().size() < totalCount) {
            waitProfit_list.setPullLoadEnable(true);
        } else {
            waitProfit_list.setPullLoadEnable(false);
        }
        waitProfit_list.setAdapter(adapter);
        onLoad();
    }

    private void onLoad() {
        waitProfit_list.stopRefresh();
        waitProfit_list.stopLoadMore();
    }
}
