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


import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseFragment;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.fragment.from.CpActivity;
import com.bonait.bnframework.modules.home.fragment.from.DiyActivity;
import com.bonait.bnframework.modules.home.fragment.from.SzActivity;
import com.bonait.bnframework.modules.home.fragment.from.WhActivity;
import com.orhanobut.logger.Logger;
import com.qmuiteam.qmui.widget.QMUICollapsingTopBarLayout;
import com.qmuiteam.qmui.widget.QMUITopBar;

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

/**
 * A simple {@link Fragment} subclass.
 */
public class Home2Fragment extends BaseFragment {

    @BindView(R.id.collapsing_topbar_layout)
    QMUICollapsingTopBarLayout mCollapsingTopBarLayout;
    @BindView(R.id.topbar)
    QMUITopBar mTopBar;
    private Context context;
    public Home2Fragment() {
    }

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

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

    private void initTopBar() {
        mCollapsingTopBarLayout.setTitle("功能菜单");
    }

    @OnClick({R.id.kshr,R.id.diy,R.id.cp,R.id.wh,R.id.sz})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.kshr://开始制作商品
                MessageManager.getInstance().sendMessage(MessageName.SelectZY,"Open");
                ToastUtils.info("打开烹饪界面");
                break;
            case R.id.diy://diy模式
                skipToActivity(DiyActivity.class);
                ToastUtils.info("打开diy界面");
                break;
            case R.id.cp://菜谱
                skipToActivity(CpActivity.class);
                ToastUtils.info("打开菜谱界面");
                break;
            case R.id.wh://维护界面
                skipToActivity(WhActivity.class);
                ToastUtils.info("打开维护界面界面");
                break;
            case R.id.sz://系统设置界面
                skipToActivity(SzActivity.class);
                ToastUtils.info("打开系统设置界面");
                break;
        }
    }

    /**
     * 跳转界面
     */
    private void skipToActivity(Class da) {
        // 跳转到登录页面
        Intent intent = new Intent(context, da);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

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

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