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.LinearLayoutManager;

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

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.Res_PLCADDRESS1;
import com.bonait.bnframework.common.helper.WrapContentLinearLayoutManager;
import com.bonait.bnframework.modules.home.adapter.wdsz_adapter;
import com.bonait.bnframework.modules.home.fragment.mode.sdkz_control;
import com.bonait.bnframework.modules.home.fragment.mode.tab_control;
import com.orhanobut.logger.Logger;

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

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

public class JiaoYan_sdkz_fragment extends BaseFragment {

    @BindView(R.id.sdkz_linrarlaout)
    LinearLayout sdkz_linrarlaout;//手动控制
    private Context context;
    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_jiaoyan_sdkz, 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();
        InitView();
    }
    /**
     * 初始化界面
     */
    public void InitView() {
        Initdata();
    }


//    @OnClick({R.id.add_wl})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.add_wl:
//
//                break;
//        }
//    }


    //region 数据加载
    /**
     * 初始化数据加载
     */
    public void Initdata() {
        try {
            Activity activity=getActivity();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                List<Res_PLCADDRESS1> plcaddresses=new ArrayList<>();
                                if (ConfigName.getInstance().versionSelectionEnum.equals("配料吧台")) {
                                    for (Res_PLCADDRESS1 item : ConfigName.getInstance().PlcAddress_配料吧台_手动) {
                                        if(item.address.equals("-------------"))//这是标题
                                        {
                                            if(plcaddresses.size()>0)
                                            {
                                                sdkz_control sdkz = new sdkz_control(sdkz_linrarlaout.getContext(), null, plcaddresses,activity,true);
                                                sdkz_linrarlaout.addView(sdkz);
                                            }
                                            plcaddresses=new ArrayList<>();
                                            tab_control tab = new tab_control(sdkz_linrarlaout.getContext(), null, item.name);
                                            sdkz_linrarlaout.addView(tab);
                                        }else
                                        {
                                            plcaddresses.add(item);
                                        }
                                    }
                                    if(plcaddresses.size()>0)
                                    {
                                        sdkz_control sdkz = new sdkz_control(sdkz_linrarlaout.getContext(), null, plcaddresses,activity,true);
                                        sdkz_linrarlaout.addView(sdkz);
                                    }
                                }else
                                {
                                    for (Res_PLCADDRESS1 item : ConfigName.getInstance().PlcAddress_奶茶机_手动) {
                                        if(item.address.equals("-------------"))//这是标题
                                        {
                                            if(plcaddresses.size()>0)
                                            {
                                                sdkz_control sdkz = new sdkz_control(sdkz_linrarlaout.getContext(), null, plcaddresses,activity,true);
                                                sdkz_linrarlaout.addView(sdkz);
                                            }
                                            plcaddresses=new ArrayList<>();
                                            tab_control tab = new tab_control(sdkz_linrarlaout.getContext(), null, item.name);
                                            sdkz_linrarlaout.addView(tab);
                                        }else
                                        {
                                            plcaddresses.add(item);
                                        }
                                    }
                                    if(plcaddresses.size()>0)
                                    {
                                        sdkz_control sdkz = new sdkz_control(sdkz_linrarlaout.getContext(), null, plcaddresses,activity,true);
                                        sdkz_linrarlaout.addView(sdkz);
                                    }
                                }
                            }
                        });

                    } catch (Exception e) {

                    }
                }
            }).start();

        } catch (Exception e) {

        }
    }
    //endregion

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.d("我的fragment销毁");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ExecuteTheRecipe.WritePLC("手自切换", true, null);
                } catch (Exception e) {

                }
            }
        }).start();
    }

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