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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ConfigData;
import com.bonait.bnframework.common.base.BaseFragment;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_PLCADDRESS;
import com.bonait.bnframework.common.modbus.ModbusTcpServer;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.NetworkUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.fragment.mode.fragment_gx;
import com.bonait.bnframework.modules.home.fragment.mode.fragment_plc;
import com.orhanobut.logger.Logger;
import com.qmuiteam.qmui.widget.QMUILoadingView;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SystemCsPLCFragment extends BaseFragment {
    @BindView(R.id.plcview)
    LinearLayout plcview;//工序子集
    private Context context;
    @BindView(R.id.load_view)
    QMUILoadingView load_view;//工序子集
    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_system_cs_plc, 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();
        Init();
    }

    /**
     * 初始化
     */
    public void Init() {
        load_view.setVisibility(View.VISIBLE);
//        QMUITipDialog loadingDialog = new QMUITipDialog.Builder(getActivity())
//                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
//                .setTipWord("正在加载...")
//                .create();
//        loadingDialog.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Activity activity = findActivity(context);
                    if (activity != null) {
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    ArrayList<BPA_PLCADDRESS> arrayList = QueryDB.GetPlcaddressALL();
                                    for (BPA_PLCADDRESS item : arrayList) {
                                        fragment_plc gongxu = new fragment_plc(context, null, item);
                                        plcview.addView(gongxu);
                                    }
                                } catch (Exception e) {
                                }finally {
                                    load_view.setVisibility(View.INVISIBLE);
                                }
                            }
                        });
                    }
                } catch (Exception e) {

                }
            }
        }).start();

    }

    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;
        }
    }

    @OnClick({R.id.chushihua})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.chushihua:
                String title = "温馨提示！";
                String message = "客官确定要初始化PLC地址吗，小菠萝温馨提示你,此操作将会影响到实际运行哦？";
                AlertDialogUtils.showDialog(context, title, message, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        load_view.setVisibility(View.VISIBLE);
                        ConfigData.getInstance().RevertPLC();
                        Init();
                        ToastUtils.info("初始化成功！");
                        dialog.dismiss();
                    }
                });
                break;

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.d("我的fragment销毁");
    }

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