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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.Context;
import android.content.Intent;
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.MessageName;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.manager.ActivityLifecycleManager;
import com.bonait.bnframework.modules.home.fragment.from.BunkerSetupActivity;
import com.bonait.bnframework.modules.home.fragment.from.CpxzActivity;
import com.bonait.bnframework.modules.home.fragment.from.DishTestActivity;
import com.bonait.bnframework.modules.home.fragment.mode.MyStatus;
import com.bonait.bnframework.modules.welcome.activity.LoginActivity;
import com.orhanobut.logger.Logger;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

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

public class Home4Fragment extends BaseFragment {

    @BindView(R.id.yauser_gn)
    LinearLayout yauser_gn;//
    private Context context;

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        context = getContext();
        initTopBar();
        initData();
    }

    /**
     * 初始化TopBar
     */
    private void initTopBar() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.kaishipengren, R.id.shebeikongzhi, R.id.zhuxiaodenglu
            , R.id.liaochangshezhi, R.id.caipinyanfa,})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.kaishipengren:
                MessageManager.getInstance().sendMessage(MessageName.SelectZY, 0);
                break;
            case R.id.shebeikongzhi:
                MessageManager.getInstance().sendMessage(MessageName.SelectZY, 2);
                break;
            case R.id.liaochangshezhi:
                OpenActivity(BunkerSetupActivity.class);
                break;
            case R.id.caipinyanfa:
                if (!ConfigName.getInstance().PlcIsConnect) {
                    ToastUtils.warning("PLC未准备就绪!!!");
                    return;
                }

                Object chushiover = ExecuteTheRecipe.getListingValue("初始化完成");
                //Object jiting = ExecuteTheRecipe.getListingValue("设备急停");
                if (chushiover == null || !(boolean) chushiover) {
                    ToastUtils.warning("设备未初始化,请先手动初始化设备!!!");
                    return;
                }
                OpenActivity(DishTestActivity.class);
                break;
            case R.id.zhuxiaodenglu:
                String title = "温馨提示！";
                String message = "客官确定要退出程序吗，小菠萝会想你的哦？";
                AlertDialogUtils.showDialog(context, title, message, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        skipToLoginActivity();
                        dialog.dismiss();
                    }
                });
                break;
        }
    }

    /**
     * 跳转登录界面
     */
    private void skipToLoginActivity() {
        // 跳转到登录页面
        Intent intent = new Intent(context, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

        getActivity().finish();
        //overridePend1ingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        // 结束所有Activity
        ActivityLifecycleManager.get().finishAllActivity();
    }

    /**
     * 初始化显示数据
     */
    private void initData() {
        if (ConfigName.getInstance().user.userID.equals("研发用户")) {
            yauser_gn.setVisibility(View.VISIBLE);
        } else {
            yauser_gn.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.d("第一页销毁");
    }

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

}