package com.danding.yang800;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.danding.yang800.Adapter.AllrderAdapter;
import com.danding.yang800.Presenter.OrderActivityPresenter;
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 OrderManageActivity extends BaseActivity implements XListView.IXListViewListener {
    @BindView(R.id.set_backTv)
    TextView set_backTv;
    @BindView(R.id.myorder_all)
    LinearLayout myorder_all;
    @BindView(R.id.myorder_waitpay)
    LinearLayout myorder_waitpay;
    @BindView(R.id.myorder_waitsend)
    LinearLayout myorder_waitsend;
    @BindView(R.id.myorder_waitcollect)
    LinearLayout myorder_waitcollect;
    @BindView(R.id.myorder_allTv)
    TextView myorder_allTv;
    @BindView(R.id.myorder_waitpayTv)
    TextView myorder_waitpayTv;
    @BindView(R.id.myorder_waitsendTv)
    TextView myorder_waitsendTv;
    @BindView(R.id.myorder_waitcollectTv)
    TextView myorder_waitcollectTv;
    @BindView(R.id.myorder_allIv)
    TextView myorder_allIv;
    @BindView(R.id.myorder_waitpayIv)
    TextView myorder_waitpayIv;
    @BindView(R.id.myorder_waitsendIv)
    TextView myorder_waitsendIv;
    @BindView(R.id.myorder_waitcollectIv)
    TextView myorder_waitcollectIv;
    private NetworkProgressDialog pd;
    @BindView(R.id.orderManage_list)
    XListView orderManage_list;
    boolean ifAll, ifpay, ifsend, ifcollect = false;
    OrderActivityPresenter presenter;
    private int Count = 0;
    AllrderAdapter adapter;
    int page = 1;
    @BindView(R.id.orderManage_noOrderLayout)
    RelativeLayout orderManage_noOrderLayout;
    String storeId = "";
    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_order_manage);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        ButterKnife.bind(this);
        storeId = sp.getString("storeId", "");
//        storeId = "300500";
        adapter = new AllrderAdapter(this);
        presenter = new OrderActivityPresenter(this, adapter);
        initView();
        ifAll = true;
        presenter.getOrderInfo("0", storeId, page + "", false);
    }

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

    android.widget.AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {

        }
    };

    @OnClick({R.id.set_backTv, R.id.myorder_all, R.id.myorder_waitpay, R.id.myorder_waitsend, R.id.myorder_waitcollect})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.set_backTv:
                finish();
                break;
            case R.id.myorder_all:
                if (!ifAll) {
                    orderManage_noOrderLayout.setVisibility(View.GONE);
                    myorder_allTv.setTextColor(ContextCompat.getColor(this, R.color.pink));
                    myorder_waitpayTv.setTextColor(ContextCompat.getColor(this, R.color.huise));
                    myorder_waitsendTv.setTextColor(ContextCompat.getColor(this, R.color.huise));
                    myorder_waitcollectTv.setTextColor(ContextCompat.getColor(this, R.color.huise));
                    myorder_allIv.setVisibility(View.VISIBLE);
                    myorder_waitpayIv.setVisibility(View.GONE);
                    myorder_waitsendIv.setVisibility(View.GONE);
                    myorder_waitcollectIv.setVisibility(View.GONE);
                    page = 1;
                    onLoad();
                    initDialog();
                    presenter.getOrderInfo("0", storeId, page + "", false);
                }

                setChecked(true, false, false, false);
                break;
            case R.id.myorder_waitpay:
                if (!ifpay) {
                    orderManage_noOrderLayout.setVisibility(View.GONE);
                    myorder_allTv.setTextColor(ContextCompat.getColor(this, R.color.huise));
                    myorder_waitpayTv.setTextColor(ContextCompat.getColor(this, R.color.pink));
                    myorder_waitsendTv.setTextColor(ContextCompat.getColor(this, R.color.huise));
                    myorder_waitcollectTv.setTextColor(ContextCompat.getColor(this, R.color.huise));
                    myorder_allIv.setVisibility(View.GONE);
                    myorder_waitpayIv.setVisibility(View.VISIBLE);
                    myorder_waitsendIv.setVisibility(View.GONE);
                    myorder_waitcollectIv.setVisibility(View.GONE);
                    page = 1;
                    onLoad();
                    initDialog();
                    presenter.getOrderInfo("1", storeId, page + "", false);
                }
                setChecked(false, true, false, false);

                break;
            case R.id.myorder_waitsend:
                if (!ifsend) {
                    orderManage_noOrderLayout.setVisibility(View.GONE);
                    myorder_allTv.setTextColor(ContextCompat.getColor(this, R.color.huise));
                    myorder_waitpayTv.setTextColor(ContextCompat.getColor(this, R.color.huise));
                    myorder_waitsendTv.setTextColor(ContextCompat.getColor(this, R.color.pink));
                    myorder_waitcollectTv.setTextColor(ContextCompat.getColor(this, R.color.huise));
                    myorder_allIv.setVisibility(View.GONE);
                    myorder_waitpayIv.setVisibility(View.GONE);
                    myorder_waitsendIv.setVisibility(View.VISIBLE);
                    myorder_waitcollectIv.setVisibility(View.GONE);
                    page = 1;
                    onLoad();
                    initDialog();
                    presenter.getOrderInfo("26", storeId, page + "", false);
                }
                setChecked(false, false, true, false);

                break;
            case R.id.myorder_waitcollect:
                if (!ifcollect) {
                    orderManage_noOrderLayout.setVisibility(View.GONE);
                    myorder_allTv.setTextColor(ContextCompat.getColor(this, R.color.huise));
                    myorder_waitpayTv.setTextColor(ContextCompat.getColor(this, R.color.huise));
                    myorder_waitsendTv.setTextColor(ContextCompat.getColor(this, R.color.huise));
                    myorder_waitcollectTv.setTextColor(ContextCompat.getColor(this, R.color.pink));
                    myorder_allIv.setVisibility(View.GONE);
                    myorder_waitpayIv.setVisibility(View.GONE);
                    myorder_waitsendIv.setVisibility(View.GONE);
                    myorder_waitcollectIv.setVisibility(View.VISIBLE);
                    page = 1;
                    onLoad();
                    initDialog();
                    presenter.getOrderInfo("1416", storeId, page + "", false);
                }
                setChecked(false, false, false, true);
                break;


        }
    }

    public void setChecked(boolean all, boolean pay, boolean send, boolean collect) {
        ifAll = all;
        ifpay = pay;
        ifsend = send;
        ifcollect = collect;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        switch (requestCode) {
//        }
    }

    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 (ifAll) {
            presenter.getOrderInfo("0", storeId, page + "", false);
        } else if (ifpay) {
            presenter.getOrderInfo("1", storeId, page + "", false);
        } else if (ifsend) {
            presenter.getOrderInfo("2", storeId, page + "", false);
        } else if (ifcollect) {
            presenter.getOrderInfo("6", storeId, page + "", false);
        }
    }

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

    public void updateAdapter(int pageTotal) {
        Count = pageTotal;
        if (page < pageTotal) {
            orderManage_list.setPullLoadEnable(true);
        } else {
            orderManage_list.setPullLoadEnable(false);
        }
        if (page==1){
            orderManage_list.setAdapter(adapter);
        }
//        orderManage_list.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        onLoad();
        if (pageTotal == 0) {
            orderManage_list.setVisibility(View.GONE);
            orderManage_noOrderLayout.setVisibility(View.VISIBLE);
        } else {
            orderManage_list.setVisibility(View.VISIBLE);
            orderManage_noOrderLayout.setVisibility(View.GONE);
        }
    }

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