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

import android.app.Activity;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.base.BaseFragment;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.res.StatusMode;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.common.view.MyLayoutManager;
import com.bonait.bnframework.modules.home.adapter.lc_adapter;
import com.bonait.bnframework.modules.home.adapter.monitor_adapter;
import com.orhanobut.logger.Logger;

import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * plc 监控
 */
public class SystemCsMonitorFragment extends BaseFragment {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;//工序子集

    @BindView(R.id.jiaobandianji)
    TextView jiaobandianji;//工序子集

    @BindView(R.id.fanzhuandianji)
    TextView fanzhuandianji;//工序子集


    private Context context;

    monitor_adapter adapter;
    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_system_cs_monitor, null);
        ButterKnife.bind(this, root);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Logger.d("我的fragment创建");
        context = getContext();
        initData();
    }

    /**
     * 初始化显示数据
     */
    private void initData() {
        try {
            MyLayoutManager layout = new MyLayoutManager();
            layout.setAutoMeasureEnabled(true);
            recyclerView.setLayoutManager(layout);

            adapter = new monitor_adapter(getContext());
            recyclerView.setAdapter(adapter);

            Run();
        } catch (Exception e) {
            ToastUtils.info("异常信息:" + e.getMessage());
        }
    }
    /**
     * 实时显示线程
     */
    public void Run() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Activity activity=getActivity();
                        if(activity!=null)
                        {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        for (StatusMode item:ConfigName.getInstance().statusModes)
                                        {
                                            if(item.Name.contains("炒锅加热异常") )
                                            {
                                                Object K= ExecuteTheRecipe.ReadPLC("异常加热故障信息");
                                                item.Status=  K == null ? false : (boolean) K;
                                            }else
                                            {
                                                item.Status=  ExecuteTheRecipe.getConcurrentHash(item.sort);
                                            }
                                        }

                                        jiaobandianji.setText( ConfigName.getInstance().statusModes_电机.get(ExecuteTheRecipe.getConcurrentHash_dj("搅拌电机")));
                                        fanzhuandianji.setText( ConfigName.getInstance().statusModes_电机.get(ExecuteTheRecipe.getConcurrentHash_dj("翻转电机")));

                                        adapter.refresh();
                                    } catch (Exception e) {

                                    }
                                }
                            });
                        }
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        ToastUtils.info("异常信息:" + e.getMessage());
                    }
                }
            }
        }).start();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.d("我的fragment销毁");
    }

    /**
     * 当在activity设置viewPager + BottomNavigation + fragment时，
     * 为防止viewPager左滑动切换界面，与fragment左滑返回上一界面冲突引起闪退问题，
     * 必须加上此方法，禁止fragment左滑返回上一界面。
     *
     * 切记！切记！切记！否则会闪退！
     *
     * 当在fragment设置viewPager + BottomNavigation + fragment时，则不会出现这个问题。
     */
    @Override
    protected boolean canDragBack() {
        return false;
    }
}