import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/page/main/main_cubit.dart';
import 'package:e_commerce_training/src/util/logger.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';

AppBar appBar(
  BuildContext context,
  String title, {
  List<IconData>? rightIcons,
  IconData? leftIcon,
  List<VoidCallback>? rightCallbacks,
  VoidCallback? leftCallback,
}) {
  MainCubit _mainCubit;
  int countProduct = 0;
  try {
    _mainCubit = BlocProvider.of<MainCubit>(context);
    countProduct = _mainCubit.countProduct;
  } catch (e) {
    // logger.e(e.toString());
  }

  return AppBar(
    backgroundColor: R.color.primaryColor,
    centerTitle: true,
    elevation: 0,
    leading: leftIcon == null
        ? null
        : IconButton(
            icon: Icon(
              leftIcon,
              size: 28,
            ),
            onPressed: () => leftCallback!(),
          ),
    title: Text(
      title,
      textAlign: TextAlign.center,
      style: TextStyle(color: R.color.white, fontSize: 22.sp, fontWeight: FontWeight.bold),
    ),
    actions: rightIcons == null
        ? null
        : rightIcons
            .map((e) => IconButton(
                  icon: e == MdiIcons.cartOutline
                      ? Stack(
                          children: [
                            Icon(
                              e,
                              color: R.color.white,
                              size: 25,
                            ),
                            Visibility(
                              visible: countProduct > 0,
                              child: Positioned(
                                right: 0,
                                child: Container(
                                  alignment: Alignment.center,
                                  padding: EdgeInsets.all(2),
                                  decoration: BoxDecoration(
                                    color: R.color.red,
                                    borderRadius: BorderRadius.circular(9),
                                  ),
                                  constraints: BoxConstraints(
                                    minWidth: 17,
                                    minHeight: 17,
                                  ),
                                  child: Text(
                                    countProduct > 0 ? countProduct.toString() : "",
                                    style: TextStyle(
                                      color: R.color.white,
                                      fontSize: 10,
                                    ),
                                    textAlign: TextAlign.center,
                                  ),
                                ),
                              ),
                            )
                          ],
                        )
                      : Icon(
                          e,
                          color: R.color.white,
                          size: 28,
                        ),
                  onPressed: () => rightCallbacks![rightIcons.indexOf(e)](),
                ))
            .toList(),
  );
}
