package com.example.bpa.view.from;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.bpa.R;
import com.example.bpa.app.BusinessServer;
import com.example.bpa.config.ConfigName;
import com.example.bpa.config.MessageName;
import com.example.bpa.db.QueryDB;
import com.example.bpa.db.mode.BPA_GOODS;
import com.example.bpa.helper.Json;
import com.example.bpa.helper.T;
import com.example.bpa.message.MessageLooper;
import com.example.bpa.message.MessageManager;
import com.example.bpa.view.adapter.sp_adapter;
import com.example.bpa.view.inteface.MyClickListener;

import java.util.ArrayList;

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

    //region 操作变量
    /**
     * 增加按钮
     */
    Button buttonaddpf;
    /**
     * 表格显示
     */
    ListView datatab;
    /**
     * 商品数据
     */
    ArrayList<BPA_GOODS> bpa_goods=new ArrayList<>();
    //endregion

    //region 私有函数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yfpf);
        Init();
        initEvents();
    }
    //endregion

    //region 公共函数

    /**
     * 初始化
     */
    private void Init(){
        gongneng_fanhui = this.findViewById(R.id.gongneng_fanhui);
        gongneng_title = this.findViewById(R.id.gongneng_title);
        buttonaddpf=this.findViewById(R.id.buttonaddpf);
        datatab= this.findViewById(R.id.datatab);
        //通过Activity.getIntent()获取当前页面接收到的Intent。 getXxxExtra方法获取Intent传递过来的数据
        String msg=getIntent().getStringExtra("data");
        gongneng_title.setText(msg);
        Initdata();//初始化数据
        RegisterMessage();//消息中心事件接收
    }

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

    /**
     * 初始化数据加载
     */
    public void Initdata()
    {
        try{
            bpa_goods= QueryDB.GetGoodsALL();
            sp_adapter adapter = new sp_adapter(yfpf_activity.this, R.layout.sp_item, bpa_goods,this);
            datatab.setAdapter(adapter);
        }catch(Exception e){

        }
    }
    //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.buttonaddpf://新建配方
                Intent intent = new Intent(this, add_pf_activity.class);
                intent.putExtra("title", "新建配方");
                intent.putExtra("data", "");
                startActivity(intent);
                break;
        }
    }

    /**
     * 接口方法，响应ListView按钮点击事件
     */
    @Override
    public void clickListener(View v,Object data) {
        switch (v.getId())
        {
            case R.id.button_item://删除按钮
                int sort=((BPA_GOODS)data).sort;
                String id=((BPA_GOODS)data).id;
                Log.i("日志",sort + "--- "+id);
                QueryDB.DeleteGoods((BPA_GOODS)data);
                Initdata();
                for (BPA_GOODS k:bpa_goods) {
                    if(k.sort>sort)
                    {
                        QueryDB.GetGoodsSort(k,1);
                    }
                }
                Initdata();
                break;
            case R.id.button_item_Up://上移动按钮
                BPA_GOODS good=(BPA_GOODS)data;
                if(good.sort==1)
                {
                    T.show(this, "已是最顶部!");
                    return;
                }
                QueryDB.GetGoodsSort((BPA_GOODS)data,1);
                Initdata();
                break;
            case R.id.button_item_Down://下移动按钮
                BPA_GOODS good2=(BPA_GOODS)data;
                if(good2.sort==bpa_goods.size())
                {
                    T.show(this, "已是最底部!");
                    return;
                }
                QueryDB.GetGoodsSort((BPA_GOODS)data,0);
                Initdata();
                break;
            case R.id.button_item_update://修改按钮
                Intent intent = new Intent(this, add_pf_activity.class);
                intent.putExtra("title", "修改配方");
                intent.putExtra("data",new Json().objectToJson(BPA_GOODS.class,data));
                startActivity(intent);
                break;
            case R.id.button_xf://下发按钮
                BusinessServer.get().AddCommodity(((BPA_GOODS)data).id);
                T.show(this, "下发成功!");
                break;
        }
    }
    //endregion

    //region 接收事件
    public void RegisterMessage()
    {
        //配方界面关闭事件
        MessageManager.getInstance().registerMessageReceiver(this, MessageName.PF_From_Close.name(), new MessageLooper.OnMessageListener() {
            @Override
            public void onMessage(Object msg) {
                if (msg != null) {
                    if(msg.equals("Close"))
                    {
                        Initdata();
                    }
                }else
                {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            T.show(yfpf_activity.this,"服务器异常，请稍后重试!");
                        }
                    });
                }
            }
        });

        MessageManager.getInstance().registerMessageReceiver(this, MessageName.PF_From_Close.name(), new MessageLooper.OnMessageListener() {
            @Override
            public void onMessage(Object msg) {
                if (msg != null) {
                    if(msg.equals("Close"))
                    {
                        Initdata();
                    }
                }else
                {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            T.show(yfpf_activity.this,"服务器异常，请稍后重试!");
                        }
                    });
                }
            }
        });
    }
    //endregion
}