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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

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

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseFragment;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_PLCADDRESS;
import com.bonait.bnframework.modules.home.fragment.mode.fragment_plc_control;
import com.orhanobut.logger.Logger;
import com.qmuiteam.qmui.widget.QMUILoadingView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PlcControl_kz_fragment extends BaseFragment {

    @BindView(R.id.plcview)
    LinearLayout plcview;//工序子集

    @BindView(R.id.load_view)
    QMUILoadingView load_view;//工序子集

    private Context context;
    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_plccontrol_kz, 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);
        plcview.removeAllViews();
        ArrayList<BPA_PLCADDRESS> arrayList = QueryDB.GetPlcaddressALL();
        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 {
                                    for (BPA_PLCADDRESS item : arrayList) {
                                        fragment_plc_control gongxu = new fragment_plc_control(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;
        }
    }
    @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;
    }
}