package com.danding.yang800;

import android.content.Context;
import android.content.SharedPreferences;
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.AccountDetailedAdapter;
import com.danding.yang800.Presenter.AccountDetailedActivityPresenter;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.View.NetworkProgressDialog;
import com.danding.yang800.View.XListView;
import com.yanzhenjie.sofia.Sofia;

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

public class AccountDetailedActivity extends BaseActivity implements XListView.IXListViewListener {
    @BindView(R.id.account_detailed_List)
    XListView account_detailed_List;
    AccountDetailedAdapter adapter;
    @BindView(R.id.accountDetailed_backTv)
    TextView accountDetailed_backTv;
    AccountDetailedActivityPresenter presenter;
    private NetworkProgressDialog pd;
    int page = 1;
    String storeId = "";
    private int Count = 0;
    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        int type = StatusColorUtil.StatusBarLightMode(this);
//        if (type > 0) {
//            StatusColorUtil.setStatusBarColor(this, R.color.white);
//            StatusColorUtil.StatusBarLightMode(this, type);
//        }
        setContentView(R.layout.activity_account_detailed);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        ButterKnife.bind(this);
        storeId = sp.getString("storeId", "");
        account_detailed_List.setPullRefreshEnable(false);
        account_detailed_List.setPullLoadEnable(true);
        account_detailed_List.setAutoLoadEnable(true);
        account_detailed_List.setXListViewListener(this);
        adapter = new AccountDetailedAdapter(this);
        presenter = new AccountDetailedActivityPresenter(this, adapter);
        presenter.accountDetail(storeId, page + "");
        // account_detailed_List.removeFootView();
    }

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

    }

    @Override
    public void onRefresh() {
        page = 1;
        onLoad();
        if (adapter != null) {
            adapter.clear();
        }
        presenter.accountDetail(storeId, page + "");
    }

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

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

    public void initDialog() {
        if (pd == null) {
            pd = NetworkProgressDialog.createDialog(this);
            if (!pd.isShowing()) {
                pd.show();
            }
        }
    }

    public void dismissDialog() {
        if (pd != null) {
            pd.dismiss();
            pd = null;
            NetworkProgressDialog.clear();
        }
    }

    public void upadateAdapter(int totalCount) {
        Count = totalCount;
        if (adapter.getLists().size() > 0 && adapter.getLists().size() != totalCount) {
            account_detailed_List.setPullLoadEnable(true);
        } else {
            account_detailed_List.setPullLoadEnable(false);
        }
        account_detailed_List.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        onLoad();
    }
}
