package com.bonait.bnframework.modules.home.fragment.mode;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODPROPERTY;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPENAME;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_ORDER;
import com.bonait.bnframework.common.db.mode.BPA_SUBORDER;
import com.bonait.bnframework.common.db.res.ResGoodProperty;
import com.bonait.bnframework.common.db.res.ResGoodsRecipe;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.helper.WrapContentLinearLayoutManager;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.common.view.MyLayoutManager;
import com.bonait.bnframework.modules.home.adapter.newneatril_adapter;
import com.bonait.bnframework.modules.home.adapter.pfsx_adapter;
import com.bonait.bnframework.modules.home.adapter.xxpf_adapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class add_makegood_control extends LinearLayout{


    @BindView(R.id.quzhizuo)
    Button quzhizuo;

    @BindView(R.id.close_from_good)
    Button close_from_good;

    public BPA_GOODS Good = null;

    @BindView(R.id.datatab_makesx)
    RecyclerView datatab_makesx;//属性

    @BindView(R.id.makegoodtitle)
    TextView makegoodtitle;//配方名称

    @BindView(R.id.chubeiliang)
    TextView chubeiliang;

    /**
     * 点击事件
     */
    public MyClickListener mListener = null;
    private View root;

    private Context contextMian;

    public add_makegood_control(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        contextMian=context;
        root = LayoutInflater.from(context).inflate(R.layout.dialog_add_makegood, this);
        ButterKnife.bind(this, root);
        Init();
    }


    public void Init() {
        //初始化界面控件的事件
        initEvent();
    }

    ArrayList<ResGoodProperty> bpa_goodproperties = new ArrayList<>();

    private void initData() {
        //2.填充规则数据，查询所有属性大类
//        LinearLayoutManager layoutManager=new LinearLayoutManager(getContext());
//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        datatab_makesx.setLayoutManager(new WrapContentLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL,false));

        //if(DataBus.getInstance().sxadapter==null)
        {
            bpa_goodproperties.clear();
            for (ResGoodProperty pro:DataBus.getInstance().bpa_goodproperties)
            {
                if(pro.GoodsTypeId!=null && !pro.GoodsTypeId.isEmpty())
                {
                    if(pro.GoodsTypeId.contains(Good.goodtype))
                    {
                        bpa_goodproperties.add(pro);
                    }
                }else
                {
                    bpa_goodproperties.add(pro);
                }
            }

            for (ResGoodProperty item:bpa_goodproperties)
            {
                int i = 0;
                for (ResGoodProperty c : item.child) {
                    if (i == 0) {
                        c.isSelect = true;
                    }else
                    {
                        c.isSelect =false;
                    }
                    i++;
                }
            }
            DataBus.getInstance().sxadapter = new pfsx_adapter(contextMian,bpa_goodproperties);
        }

        datatab_makesx.setAdapter(DataBus.getInstance().sxadapter);
    }

    /**
     * 设置数据
     *
     * @param
     */
    public void SetData(BPA_GOODS good) {
        try {
            Good = good;
            makegoodtitle.setText(Good.name + "");
            chubeiliang.setText(ExecuteTheRecipe.MakeGoodCount +"");
            initData();
        } catch (Exception ex) {

        }
    }


    private void initEvent() {
        quzhizuo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null && SaveData()) {

                    mListener.clickListenerNew(view, 1, null);
                }
            }
        });
        close_from_good.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.clickListenerNew(v, 0, null);
                }
            }
        });
    }

    /**
     * 保存数据
     */
    public boolean SaveData() {
        if (DataValidation()) {

            BPA_ORDER order = new BPA_ORDER();
            order.thirdPartyID = "手动下单";
            order.status = 0;
            order.deviceID = ConfigName.getInstance().DeviceId;
            order.userID = ConfigName.getInstance().user.userID;
            QueryDB.AddOrder(order);

            BPA_SUBORDER suborder = new BPA_SUBORDER();
            suborder.deviceID = ConfigName.getInstance().DeviceId;
            suborder.userID = ConfigName.getInstance().user.userID;
            suborder.orderID = order.id;
            suborder.goodsID = ((BPA_GOODS) Good).id;
            suborder.recipeID=goodsrecipename.id;
            suborder.number = 1;
            suborder.status = 0;
            suborder.exp=names;
            QueryDB.AddSubOrder(suborder);

            DataBus.getInstance().AddGoodsMake(suborder);
            ToastUtils.info("商品:"+Good.name+",加入队列中...");
            return true;
        }else
        {
            return false;
        }
    }

    BPA_GOODSRECIPENAME goodsrecipename = null;
    String ggids = "";//规格id集合
    String names="";
    /**
     * 数据验证
     */
    public boolean DataValidation() {
        //1.数据名称不能为空
        ggids = "";names="";
        for (ResGoodProperty item : bpa_goodproperties) {
            for (ResGoodProperty k : item.child) {
                if (k.isSelect) {
                    ggids += k.id + ",";
                    names+="/"+k.name;
                }
            }
        }

        if (!ggids.isEmpty()) {
            ggids = ggids.substring(0, ggids.length() - 1);
        }

        goodsrecipename = QueryDB.GetGoodsRecipeNameDesignId(ggids,Good.id);
        if (goodsrecipename == null) {
            ToastUtils.info("没有查询到配方,不能制作!");
            return false;
        }
        return true;
    }
}