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.ListView;
import android.widget.TextView;

import com.danding.yang800.Adapter.AccountDetailedAdapter;
import com.danding.yang800.Adapter.TakeNotesAdapter;
import com.danding.yang800.Entity.AccountDetailedInfo;
import com.danding.yang800.Entity.TakeNotesInfo;
import com.danding.yang800.Presenter.AccountDetailedActivityPresenter;
import com.danding.yang800.Presenter.TakeNotesActivityPresenter;
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 TakeNotesActivity extends BaseActivity implements XListView.IXListViewListener {
    @BindView(R.id.takeNotes_List)
    XListView takeNotes_List;
    TakeNotesAdapter adapter;
    @BindView(R.id.takeNotes_backTv)
    TextView takeNotes_backTv;
    private NetworkProgressDialog pd;
    TakeNotesActivityPresenter presenter;
    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_take_notes);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        ButterKnife.bind(this);
        storeId=sp.getString("storeId", "");
        takeNotes_List.setPullRefreshEnable(false);
        takeNotes_List.setPullLoadEnable(true);
        takeNotes_List.setAutoLoadEnable(true);
        takeNotes_List.setXListViewListener(this);
        adapter = new TakeNotesAdapter(this);
        presenter = new TakeNotesActivityPresenter(this, adapter);
        presenter.getInfo(storeId, page + "");
    }

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

    }

    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();
        }
    }

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

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

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

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