package com.example.bpa.view.fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.bpa.MainActivity;
import com.example.bpa.Model.Res_PLCADDRESS;
import com.example.bpa.R;
import com.example.bpa.config.ConfigName;
import com.example.bpa.config.DataBus;
import com.example.bpa.config.MessageName;
import com.example.bpa.db.QueryDB;
import com.example.bpa.db.mode.BPA_GOODS;
import com.example.bpa.db.mode.BPA_SUBORDER;
import com.example.bpa.helper.T;
import com.example.bpa.helper.WrapContentLinearLayoutManager;
import com.example.bpa.message.MessageLooper;
import com.example.bpa.message.MessageManager;
import com.example.bpa.service.DeviceData;
import com.example.bpa.view.adapter.maingoods_adapter;
import com.example.bpa.view.adapter.makegood_adapter;
import com.example.bpa.view.adapter.order_adapter;
import com.example.bpa.view.control.MyLayoutManager;
import com.example.bpa.view.control.sdkz_control;
import com.example.bpa.view.control.tab_control;
import com.example.bpa.view.inteface.MyClickListener;
import com.example.bpa.view.mode.MakeStatus;
import com.example.bpa.view.mode.ResGoodsMake;
import com.example.bpa.view.mode.ResGoodsRecipe;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页界面
 */
public class HomeFragment extends Fragment implements View.OnClickListener, MyClickListener {

    //region 变量
    LinearLayout makesp,nohelp;
    /**
     * 商品列表
     */
    RecyclerView good_recyclerView;
    /**
     * 商品制作列表
     */
    RecyclerView MakeGood;
    /**
     * 自动控制按钮
     */
    ImageView image_zdkz;
    /**
     * 当前温度
     */
    TextView wendu1, wendu2;
    /**
     * 界面实例
     */
    public View view;
    //endregion

    //region 私有函数
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container,false);
        Init();
        return view;
    }
    //endregion

    //region 公共函数
    /**
     * 初始化
     */
    private void Init(){

        good_recyclerView = view.findViewById(R.id.good_recyclerView);
        makesp=view.findViewById(R.id.makesp);
        nohelp=view.findViewById(R.id.nohelp);
        image_zdkz=view.findViewById(R.id.image_zdkz);
        wendu1 = view.findViewById(R.id.wendu1);
        wendu2 = view.findViewById(R.id.wendu2);

        if( ConfigName.getInstance().IsUserDownOrder)
        {
            makesp.setVisibility(View.VISIBLE);
            nohelp.setVisibility(View.GONE);
        }else
        {
            makesp.setVisibility(View.GONE);
            nohelp.setVisibility(View.VISIBLE);
        }

        //MakeGood=view.findViewById(R.id.MakeGood);
        Initdata();//初始化数据
        RegisterMessage();//消息中心事件接收
        initEvents();
        startListening();
    }
    /**
     * 初始化按钮事件
     */
    private void initEvents() {
        activity=findActivity(view.getContext());
        image_zdkz.setOnClickListener(this);
    }
    private Activity findActivity(@NonNull Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return findActivity(((ContextWrapper) context).getBaseContext());
        } else {
            return null;
        }
    }
    maingoods_adapter goodadapter;
    /**
     * 初始化数据加载
     */
    public void Initdata()
    {
        try{
            if( ConfigName.getInstance().IsUserDownOrder)
            {
                MyLayoutManager layout = new MyLayoutManager();
                layout.setAutoMeasureEnabled(true);
                good_recyclerView.setLayoutManager(layout);

                goodadapter = new maingoods_adapter( getContext());
                good_recyclerView.setAdapter(goodadapter);
                DataBus.getInstance().UpdateMainGoods();//更新商品
            }



//            MakeGood.setLayoutManager(new WrapContentLinearLayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL,false));
//            DataBus.getInstance().goodmakeadapter = new makegood_adapter(getContext());
//            MakeGood.setAdapter(DataBus.getInstance().goodmakeadapter);
        }catch(Exception e){

        }
    }
    //endregion

    //region 点击事件
    /**
     * 本页面点击事件监听22
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_zdkz://image
                ShowDialog();
                break;
        }
    }

    /**
     * 接口方法，响应ListView按钮点击事件
     */
    @Override
    public void clickListener(View v,Object data) {
        switch (v.getId())
        {
            case R.id.button_item://删除按钮

                break;

        }
    }
    AlertDialog alg = null;
    /**
     * 显示自动控制
     */
    public void ShowDialog()
    {
        try {
            if (alg == null) {
                alg = new AlertDialog.Builder(this.getContext()).create();
                if (!alg.isShowing())
                    alg.show();
                Window win = alg.getWindow();
                win.setContentView(R.layout.zdkz_dialog);

                WindowManager m = this.activity.getWindowManager();
                Display d = m.getDefaultDisplay();  //为获取屏幕宽、高

                android.view.WindowManager.LayoutParams p = win.getAttributes();  //获取对话框当前的参数值
                p.height = (int) (d.getHeight() * 1);   //高度设置为屏幕的0.3
                p.width = (int) (d.getWidth() * 1);    //宽度设置为屏幕的0.5
                win.setAttributes(p);     //设置生效

                ImageView gongneng_fanhui = win.findViewById(R.id.gongneng_fanhui);
                TextView gongneng_title = win.findViewById(R.id.gongneng_title);
                LinearLayout sdkz_linrarlaout = win.findViewById(R.id.sdkz_linrarlaout);
                gongneng_title.setText("硬件控制");

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    try {

                                        List<Res_PLCADDRESS> plcaddresses=new ArrayList<>();
                                        for (Res_PLCADDRESS item : ConfigName.getInstance().PlcAddress_配料吧_手动) {
                                            if(item.isread==1)
                                            {
                                                if(item.address.equals("-------------"))//这是标题
                                                {
                                                    if(plcaddresses.size()>0)
                                                    {
                                                        sdkz_control sdkz = new sdkz_control(getContext(), null, plcaddresses,activity,false);
                                                        sdkz_linrarlaout.addView(sdkz);
                                                    }
                                                    plcaddresses=new ArrayList<>();
                                                    tab_control tab = new tab_control(getContext(), null, item.name);
                                                    sdkz_linrarlaout.addView(tab);
                                                }else
                                                {
                                                    plcaddresses.add(item);
                                                }
                                            }
                                        }
                                        if(plcaddresses.size()>0)
                                        {
                                            sdkz_control sdkz = new sdkz_control(getContext(), null, plcaddresses,activity,false);
                                            sdkz_linrarlaout.addView(sdkz);
                                        }
                                    } catch (Exception e) {

                                    }
                                }
                            });
                        } catch (Exception e) {

                        }
                    }
                }).start();
                gongneng_fanhui.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alg.dismiss();
                    }
                });
            }else
            {
                if (!alg.isShowing())
                    alg.show();
                else
                {
                    alg.dismiss();
                    alg = null;
                }
            }

        } catch (Exception ex) {
        }
    }
    //endregion

    //region 接收事件
    Activity activity=null;
    public void RegisterMessage()
    {

    }

    /**
     * 监听外部数据变化
     */
    private void startListening() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                wendu1.setText(String.valueOf(DeviceData.Get().getBWX_FructoseTemperature()) + "°C");
                                wendu2.setText(String.valueOf(DeviceData.Get().getJRX_FructoseTemperature()) + "°C");
                            }
                        });
                        Thread.sleep(1000);

                    } catch (Exception e) {

                    }
                }
            }
        }).start();
    }
    //endregion
}