package com.example.bpa.view.from;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.bpa.R;
import com.example.bpa.config.ConfigName;
import com.example.bpa.config.MessageName;
import com.example.bpa.databinding.ActivityMainBinding;
import com.example.bpa.db.QueryDB;
import com.example.bpa.db.mode.BPA_GOODS;
import com.example.bpa.db.mode.BPA_GOODSRECIPE;
import com.example.bpa.db.mode.BPA_MATERIAL;
import com.example.bpa.db.mode.BPA_SUGAR;
import com.example.bpa.helper.Json;
import com.example.bpa.helper.T;
import com.example.bpa.message.MessageManager;
import com.example.bpa.view.adapter.pf_adapter;
import com.example.bpa.view.inteface.MyClickListener;
import com.example.bpa.view.mode.ResGoodsRecipe;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class add_pf_activity extends AppCompatActivity implements View.OnClickListener, MyClickListener {
    //region 变量
    /**
     * 返回按钮
     */
    ImageView gongneng_fanhui;
    /**
     * 标题设置
     */
    TextView gongneng_title;
    //endregion

    //region 操作变量
    /**
     * 输入框
     */
    TextView edittext;
    /**
     * 增加按钮
     */
    Button buttonsave,btn_addwuliao,btn_addguotang;

    Spinner wuliaolist_spinner,guotanglist_spinner;
    /**
     * 表格显示
     */
    ListView datatab;
    /**
     * 配方数据
     */
    ArrayList<ResGoodsRecipe> bpa_pf = new ArrayList<>();

    /**
     * 物料和果糖map
     */
    Map<String,String> material_map = new LinkedHashMap<>();
    Map<String,String> sugar_map = new LinkedHashMap<>();
    /**
     * 当前传入的商品数据
     */
    BPA_GOODS good = null;

    /**
     * 商品数据
     */
    ArrayList<BPA_GOODS> bpa_goods=new ArrayList<>();
    /**
     * wulaipo
     */
    ArrayList<BPA_MATERIAL> wuliao=new ArrayList<>();
    pf_adapter adapter=null;
    //endregion

    //region 私有函数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_pf);
        Init();
        initEvents();
    }
    //endregion

    //region 公共函数

    /**
     * 初始化
     */
    private void Init() {
        gongneng_fanhui = this.findViewById(R.id.gongneng_fanhui);
        gongneng_title = this.findViewById(R.id.gongneng_title);
        edittext = this.findViewById(R.id.edittext);
        buttonsave = this.findViewById(R.id.buttonsave);
        btn_addwuliao = this.findViewById(R.id.btn_addwuliao);
        btn_addguotang = this.findViewById(R.id.btn_addguotang);

        wuliaolist_spinner = this.findViewById(R.id.wuliaolist_spinner);
        guotanglist_spinner = this.findViewById(R.id.guotanglist_spinner);

        datatab = this.findViewById(R.id.datatab);
        //通过Activity.getIntent()获取当前页面接收到的Intent。 getXxxExtra方法获取Intent传递过来的数据
        String msg = getIntent().getStringExtra("title");
        String data = getIntent().getStringExtra("data");
        if (!data.isEmpty()) {
            T.show(this, data);

            good = new Json<BPA_GOODS>().jsonToobject(BPA_GOODS.class, data);
            edittext.setText(good.name);
        }
        gongneng_title.setText(msg);
        bpa_goods= QueryDB.GetGoodsALL();
        wuliao=QueryDB.GetMaterialALL();
        Initdata();
    }

    /**
     * 初始化按钮事件
     */
    private void initEvents() {
        gongneng_fanhui.setOnClickListener(this);
        buttonsave.setOnClickListener(this);
        btn_addwuliao.setOnClickListener(this);
        btn_addguotang.setOnClickListener(this);
    }

    /**
     * 初始化数据加载
     */
    public void Initdata() {
        bpa_pf = new ArrayList<>();
        try {
            if (good != null)//修改
            {
                T.show(this, "查询物料数据" +good.id);
                bpa_pf= QueryDB.GetGoodsSrecipeList(good.id);
                DataTab();
            }


        } catch (Exception e) {

        }
        try {
            //获取全部物料绑定物料spinner
            List<BPA_MATERIAL> resMaterials =  QueryDB.GetMaterialALL();
            for (BPA_MATERIAL material:resMaterials){
                material_map.put(material.name,material.id);
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_text_item, new ArrayList<>(material_map.keySet()));
            adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
            wuliaolist_spinner.setAdapter(adapter);

            List<BPA_SUGAR> resSugars = QueryDB.GetSugarALL();
            for (BPA_SUGAR sugar:resSugars){
                sugar_map.put(sugar.sugarName,sugar.id);
            }
            ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this, R.layout.spinner_text_item, new ArrayList<>(sugar_map.keySet()));
            adapter2.setDropDownViewResource(R.layout.spinner_dropdown_item);
            guotanglist_spinner.setAdapter(adapter2);

        }
        catch (Exception e) {

        }

    }

    /**
     * 刷新DataTab
     */
    public void DataTab()
    {
        adapter = new pf_adapter(add_pf_activity.this, R.layout.pf_item, bpa_pf,wuliao,this);
        datatab.setAdapter(adapter);
    }
    //endregion

    //region 点击事件

    /**
     * 本页面点击事件监听22
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gongneng_fanhui://返回按钮
                this.finish();
                break;

            case R.id.buttonsave://保存按钮点击
                if(adapter==null || bpa_pf.size()<=0)
                {
                    T.show(this, "配方不能为空!");
                    return;
                }
                bpa_pf=adapter.GetDatas();
                for (int i=0;i<bpa_pf.size();i++){
                    ResGoodsRecipe pe=bpa_pf.get(i);
                    if(pe.materialName.isEmpty())
                    {
                        T.show(this, "第"+i+"行,物料不能为空!");
                        return;
                    }
                }
                List<ResGoodsRecipe> kd= bpa_pf;
                if(kd.size()<=0)
                {
                    T.show(this, "配方信息不能为空!");
                    return;
                }
                String name = edittext.getText().toString();
                if (good == null)//新增
                {
                    if (name.isEmpty()) {
                        T.show(this, "商品名称不能为空!");
                        return;
                    }
                    if (QueryDB.GetGoodsIs(name)) {
                        T.show(this, "商品名称重复,请重新输入后重试!");
                        return;
                    }
                    BPA_GOODS goodx = new BPA_GOODS();
                    goodx.name = name;
                    goodx.sort = bpa_goods.size() + 1;
                    goodx.status = 0;
                    goodx.deviceID = ConfigName.getInstance().DeviceId;
                    goodx.userID = ConfigName.getInstance().user.userID;
                    QueryDB.AddGoods(goodx);
                    good=goodx;
                }else //修改
                {
                    if (name.isEmpty()) {
                        T.show(this, "商品名称不能为空!");
                        return;
                    }
                    if (!name.equals(good.name) && QueryDB.GetGoodsIs(name)) {
                        T.show(this, "商品名称重复,请重新输入后重试!");
                        return;
                    }
                    good.name=name;
                    QueryDB.UpdateGoods(good);
                    QueryDB.DeleteGoodsSrecipeList(good.id);
                }

                for (ResGoodsRecipe item:kd)
                {
                    BPA_GOODSRECIPE da=new BPA_GOODSRECIPE();
                    da.goodsID = good.id;
                    da.materialID = item.materialID;
                    da.materialType = item.materialType;
                    da.value = item.value;
                    da.sort =item.sort;
                    da.deviceID = ConfigName.getInstance().DeviceId;
                    da.userID = ConfigName.getInstance().user.userID;
                    QueryDB.AddGoodsSrecipe(da);
                }
                T.show(this, "操作成功!");
                MessageManager.getInstance().sendMessage(MessageName.PF_From_Close.name(),"Close");
                this.finish();
                break;
            case R.id.btn_addwuliao:
                ResGoodsRecipe recipe1=new ResGoodsRecipe();//new 一个空对象
                if(adapter!=null) bpa_pf=adapter.GetDatas();
                recipe1.materialType = 1;//物料类型
                recipe1.sort=bpa_pf.size()+1;
                recipe1.materialName = wuliaolist_spinner.getSelectedItem().toString();
                recipe1.materialID = material_map.get(recipe1.materialName);
                bpa_pf.add(recipe1);
                DataTab();
                break;
            case R.id.btn_addguotang:
                ResGoodsRecipe recipe=new ResGoodsRecipe();//new 一个空对象
                if(adapter!=null) bpa_pf=adapter.GetDatas();
                recipe.sort=bpa_pf.size()+1;
                recipe.materialType = 2;
                recipe.materialName = guotanglist_spinner.getSelectedItem().toString();
                recipe.materialID = sugar_map.get(recipe.materialName);
                bpa_pf.add(recipe);
                DataTab();
                break;
        }
    }

    /**
     * 接口方法，响应ListView按钮点击事件
     */
    @Override
    public void clickListener(View v, Object data) {
        ResGoodsRecipe wl=(ResGoodsRecipe)data;
        switch (v.getId()) {
            case R.id.button_item://删除
                bpa_pf.remove(wl);
                DataTab();
                break;
            case R.id.button_item_Up://上移动
                if(wl.sort==1)
                {
                    T.show(this, "已是最顶部!");
                    return;
                }
                int dq=wl.sort;
                ResGoodsRecipe qt= bpa_pf.get(dq-1);
                qt.sort=dq;
                wl.sort=dq-1;
                bpa_pf.set(dq,qt);
                bpa_pf.set(dq-1,wl);
                DataTab();
                break;
            case R.id.button_item_Down://下移动
                if(wl.sort==bpa_pf.size())
                {
                    T.show(this, "已是最底部!");
                    return;
                }
                int dq1=wl.sort;
                ResGoodsRecipe qt1= bpa_pf.get(dq1+1);
                qt1.sort=dq1;
                wl.sort=dq1+1;
                bpa_pf.set(dq1,qt1);
                bpa_pf.set(dq1+1,wl);
                DataTab();
                break;
        }
    }
    //endregion
}