import 'package:badges/badges.dart' as badge;
import 'package:elektronika/app/data/routes/app_routes.dart';
import 'package:elektronika/app/pages/cart/controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:elektronika/app/core/themes/colors.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';

class CustomAppbarWidget extends StatelessWidget implements PreferredSizeWidget {
  final Widget? leading, trailing;
  final double elevation;

  CustomAppbarWidget({
    this.leading,
    this.trailing,
    this.elevation = 0.35,
  });

  @override
  Size get preferredSize => Size.fromHeight(kToolbarHeight);

  @override
  Widget build(BuildContext context) {
    return GetX<CartController>(
      init: CartController(),
      builder: (controller) => PreferredSize(
        preferredSize: Size.fromHeight(kToolbarHeight),
        child: AppBar(
          leading: this.leading ?? null,
          // SvgPicture.asset(
          //   'assets/appbar/menu_icon.svg',
          //   // height: 12.h,
          //   // width: 12.w,
          //   fit: BoxFit.scaleDown,
          // ),
          automaticallyImplyLeading: false,
          title: SvgPicture.asset('assets/logo/logo.svg', height: 24.sp),
          elevation: this.elevation,
          actions: [
            GestureDetector(
              onTap: () =>
                  Get.toNamed(AppRoutes.CONTACTS), //debugPrint('onContactTapped'), //Get.toNamed(AppRoutes.CART),
              child: Row(
                children: [
                  Icon(Icons.call_outlined, color: Colors.grey, size: 26),
                  SizedBox(width: 16),
                ],
              ),
            ),
            GestureDetector(
              onTap: () => Get.toNamed(AppRoutes.CART),
              child: this.trailing ??
                  Row(
                    children: [
                      controller.state.cartModel.value != null /* && controller.state.cartModel.value?.itemsQty != 0 */
                          ? badge.Badge(
                              position: badge.BadgePosition.topEnd(top: -14),
                              // animationType: badge.BadgeAnimationType.scale,
                              // animationDuration: Duration(milliseconds: 300),
                              // badgeColor: ThemeColor.mainColor.withOpacity(0.70),
                              badgeAnimation: badge.BadgeAnimation.rotation(
                                animationDuration: Duration(milliseconds: 300),
                                colorChangeAnimationDuration: Duration(seconds: 1),
                                curve: Curves.fastOutSlowIn,
                                colorChangeAnimationCurve: Curves.easeInCubic,
                              ),
                              badgeStyle: badge.BadgeStyle(
                                badgeColor: ThemeColor.mainColor.withOpacity(0.70),
                              ),
                              badgeContent: Text(
                                controller.state.cartModel.value?.itemsQty.toString() ?? '',
                                style: new TextStyle(
                                  color: Colors.white,
                                  fontSize: 10.sp,
                                  fontWeight: FontWeight.w700,
                                ),
                              ),
                              child: SvgPicture.asset('assets/appbar/cart_icon.svg'),
                            )
                          : SvgPicture.asset('assets/appbar/cart_icon.svg'),
                      SizedBox(width: 16),
                    ],
                  ),
            ),
          ],
        ),
      ),
    );
  }
}

class AppBarBackBtn extends StatelessWidget {
  final VoidCallback? callback;

  AppBarBackBtn({this.callback});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(8.0),
      child: InkWell(
        customBorder: new CircleBorder(),
        onTap: () => this.callback == null ? Get.back() : this.callback!(),
        splashColor: Theme.of(context).splashColor,
        child: new Icon(
          Icons.arrow_back_ios,
          size: 20.sp,
          color: ThemeColor.grey,
        ),
      ),
    );
  }
}
