package com.philisense.rdgo.ui.waitPutIn;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.GravityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.xrecyclerview.XRecyclerView;
import com.philisense.rdgo.R;
import com.philisense.rdgo.adapter.ProductAdapter;
import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.response.ProductBean;
import com.philisense.rdgo.bean.response.TypeBean;
import com.philisense.rdgo.databinding.ActivityProductBinding;
import com.philisense.rdgo.http.RequestImpl;
import com.philisense.rdgo.viewModel.HQTViewModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class ProductActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener {

    private ActivityProductBinding mBinding;
    private HQTViewModel viewModel;
    private ProductAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_product);

        // toolbar
        Toolbar toolbar = mBinding.include.toolbar;
        toolbar.setTitle("产品");
        setSupportActionBar(toolbar);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        toolbar.setOnMenuItemClickListener(this);
        viewModel = new HQTViewModel(this);

        click();
        getData();
        getProduct();
        mBinding.lvProduct.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ProductAdapter(this);
        mBinding.lvProduct.setAdapter(adapter);
        mBinding.lvProduct.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getProduct();
            }

            @Override
            public void onLoadMore() {
                mBinding.lvProduct.refreshComplete();
            }
        });
        adapter.setOnClickListener(new ProductAdapter.OnItemClickListener() {
            @Override
            public void btnOnClick(ProductBean.ListBean s, int postion) {
                run(s);
            }
        });
    }

    private void run(ProductBean.ListBean beans) {
        Intent mIntent = new Intent();
        mIntent.putExtra("product", (Serializable) beans);
        // 设置结果，并进行传送
        this.setResult(0, mIntent);
        this.finish();

    }

    private void click() {
        mBinding.type.Reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewModel.brankProduct = "";
                viewModel.geneProduct = "";
                viewModel.levelProduct = "";
                viewModel.capacityProduct = "";
                mBinding.type.brank.chooseItemStyle(0);
                mBinding.type.gene.chooseItemStyle(0);
                mBinding.type.level.chooseItemStyle(0);
                mBinding.type.capacity.chooseItemStyle(0);
            }
        });
        mBinding.type.sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBinding.drawerlayout.closeDrawers();
                getProduct();
            }
        });
    }


    private List<ProductBean.ListBean> list;

    private void getProduct() {
        final ProgressDialog dialog = ProgressDialog.show(ProductActivity.this, "提示", "正在获取数据..");

        viewModel.getproduct(new RequestImpl<List<ProductBean.ListBean>>() {
            @Override
            public void loadSuccess(List<ProductBean.ListBean> bean) {
                dialog.dismiss();
                adapter.setData(bean);
                list = bean;
                mBinding.lvProduct.refreshComplete();

            }

            @Override
            public void loadFailed(OError error) {
                list = new ArrayList<>();
                adapter.setData(list);
                mBinding.lvProduct.refreshComplete();
                dialog.dismiss();
            }
        });

    }


    private void getData() {
        viewModel.getType(new RequestImpl<TypeBean>() {
            @Override
            public void loadSuccess(TypeBean bean) {
                setData(bean);
            }

            @Override
            public void loadFailed(OError error) {

            }
        });
    }


    private void setData(TypeBean bean) {
        List<String> bk = bean.getData().getBrand();
        bk.add(0, "全部");

        mBinding.type.brank.addItemViews(getItems(bk));
        mBinding.type.brank.setGroupClickListener(new GoodsViewGroup.OnGroupItemClickListener() {
            @Override
            public void onGroupItemClick(int itemPos, String key, String value) {
                if ("全部".equals(key)) {
                    viewModel.brankProduct = "";
                } else {
                    viewModel.brankProduct = value;
                }

            }
        });
        mBinding.type.brank.chooseItemStyle(0);
        List<String> ge = new ArrayList<>();
        ge.add("全部");
        ge.add("转基因");
        ge.add("非转基因");
        ge.add("其他");
        mBinding.type.gene.addItemViews(getItems(ge));
        mBinding.type.gene.setGroupClickListener(new GoodsViewGroup.OnGroupItemClickListener() {
            @Override
            public void onGroupItemClick(int itemPos, String key, String value) {
                if ("全部".equals(key)) {
                    viewModel.geneProduct = "";
                } else {
                    viewModel.geneProduct = (itemPos - 1) + "";
                }

            }
        });
        mBinding.type.gene.chooseItemStyle(0);
        List<String> lv = bean.getData().getLevel();
        lv.add(0, "全部");
        mBinding.type.level.addItemViews(getItems(lv));
        mBinding.type.level.setGroupClickListener(new GoodsViewGroup.OnGroupItemClickListener() {
            @Override
            public void onGroupItemClick(int itemPos, String key, String value) {
                if ("全部".equals(key)) {
                    viewModel.levelProduct = "";
                } else {
                    viewModel.levelProduct = value;
                }
            }
        });
        mBinding.type.level.chooseItemStyle(0);
        List<String> cp = bean.getData().getCapacity();
        cp.add(0, "全部");
        mBinding.type.capacity.addItemViews(getItems(cp));
        mBinding.type.capacity.setGroupClickListener(new GoodsViewGroup.OnGroupItemClickListener() {
            @Override
            public void onGroupItemClick(int itemPos, String key, String value) {
                if ("全部".equals(key)) {
                    viewModel.capacityProduct = "";
                } else {
                    viewModel.capacityProduct = value;
                }
            }
        });
        mBinding.type.capacity.chooseItemStyle(0);

    }

    private List<GoodsViewGroupItem> getItems(List<String> s) {
        List<GoodsViewGroupItem> items = new ArrayList<>();
        for (int i = 0; i < s.size(); i++) {
            items.add(new GoodsViewGroupItem(i + "", s.get(i)));
        }
        return items;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.shuaixuan, menu);
        return true;
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.screen:
                mBinding.drawerlayout.openDrawer(GravityCompat.END);
                break;

            default:
                break;

        }

        return true;
    }

    public static void start(Activity context, int num) {
        Intent intent = new Intent(context, ProductActivity.class);
        ActivityCompat.startActivityForResult(context, intent, num, null);
    }

}
