import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:ongbut_ntmanh/app/module/counter/bloc/counter_bloc.dart';
import 'package:ongbut_ntmanh/app/ui/navigationbottom/but_about_page.dart';
import 'package:ongbut_ntmanh/app/ui/navigationbottom/camera_page.dart';
import 'package:ongbut_ntmanh/app/ui/navigationbottom/home_page.dart';
import 'package:ongbut_ntmanh/res/images/images.dart';
import 'app/ui/navigationbottom/account_page.dart';
import 'app/ui/navigationbottom/but_answer_page.dart';
import 'const/colors.dart';

class Application extends StatefulWidget {

  const Application({Key? key}) : super(key: key);
  @override
  State<Application> createState() => _ApplicationState();
}
int _page = 0;

class _ApplicationState extends State<Application> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          toolbarHeight: 0,
          backgroundColor: AppColors.primary,
        ),
        body:
        _counterWidget(context),
        bottomNavigationBar: appBar(),
    );
  }
  BottomAppBar appBar(){
    return BottomAppBar(
      shape: const CircularNotchedRectangle(),
      color: Theme.of(context).cardColor.withAlpha(255),
      child: BottomNavigationBar(
        onTap: (int index){
          _onBottomNavigationBarItem(index);
          BlocProvider.of<CounterBloc>(context).add(
              NavigationBottomEvent(indexNavigationBottom: index));

        },
        currentIndex: _page,
        elevation: 0,
        unselectedItemColor: AppColors.grey,
        selectedItemColor: AppColors.primary,
        items: const [
          BottomNavigationBarItem(
            icon: ImageIcon(AssetImage(Images.home)),
            label: 'Trang Chu',
          ),
          BottomNavigationBarItem(
              icon: ImageIcon(AssetImage(Images.classes)),
              label: 'But Co',
          ),
          BottomNavigationBarItem(
              icon: ImageIcon(AssetImage(Images.camera_ai)),
              label: 'Camera',
          ),
          BottomNavigationBarItem(
            icon: ImageIcon(AssetImage(Images.social)),
            label:'But Dap',
          ),
          BottomNavigationBarItem(
              icon: ImageIcon(AssetImage(Images.user)),
              label:'Ca Nhan',
          ),
        ],
      ),
    );
  }
  void _onBottomNavigationBarItem(int index){
    setState(() {
      _page = index;
    });
  }

}

Widget _counterWidget(BuildContext context) {
  return
    BlocBuilder<CounterBloc, CounterState>(
      builder: (context, state) {
        if(state is CounterHomePageState){
          return HomePage();
        }
        if(state is CounterButAboutPageState){
          return const ButAboutPage();
        }
        if(state is CounterCameraPageState){
          return const CameraPage();
        }
        if(state is CounterButAnswerPageState){
          return const ButAnswerPage();
        }
        if(state is CounterAccountPageState){
          return const AccountPage();
        }
        return HomePage();
      }
    );
}


