import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:initial_folder/providers/carts_provider.dart';
import 'package:initial_folder/providers/notification_provider.dart';
import 'package:initial_folder/screens/home/components/appBar/icon_btn_with_counter.dart';
import 'package:initial_folder/screens/home/components/notification.dart';
import 'package:initial_folder/screens/login/login_with_email/login_email_screen.dart';
import 'package:initial_folder/size_config.dart';
import 'package:initial_folder/theme.dart';
import 'package:initial_folder/widgets/custom_navigator.dart';
import 'package:provider/provider.dart';
import 'package:initial_folder/helper/user_info.dart';
import 'package:initial_folder/screens/cart/cart_page.dart';
import 'package:initial_folder/screens/splash/splash_screen_login.dart';

class AppBarHeader extends StatelessWidget {
  const AppBarHeader({
    Key? key,
    this.idcourse,
    this.color,
  }) : super(key: key);

  final String? idcourse;
  final Color? color;

  @override
  Widget build(BuildContext context) {
    Future _showDialogNotLogin(String teks) {
      return showDialog(
        context: context,
        builder: (context) => AlertDialog(
          contentPadding: EdgeInsets.fromLTRB(12, 20, 12, 1),
          content: Text(
            'Mohon login terlebih dahulu sebelum $teks',
            style: primaryTextStyle.copyWith(
                fontSize: getProportionateScreenWidth(12), letterSpacing: 1),
          ),
          actions: [
            GestureDetector(
              onTap: () {
                Navigator.of(context).pop();
              },
              child: Text(
                'Batal',
                style: primaryTextStyle.copyWith(
                  fontSize: getProportionateScreenWidth(12),
                  letterSpacing: 1,
                  color: primaryColor,
                ),
              ),
            ),
            SizedBox(width: getProportionateScreenWidth(5)),
            GestureDetector(
              onTap: () {
                Navigator.of(context).pushNamedAndRemoveUntil(
                    LoginEmail.routeName, (Route<dynamic> route) => false);
              },
              child: Text(
                'Login',
                style: primaryTextStyle.copyWith(
                  fontSize: getProportionateScreenWidth(12),
                  letterSpacing: 1,
                  color: primaryColor,
                ),
              ),
            ),
          ],
        ),
      );
    }

    handleNotLoginCart() async {
      var token = await UsersInfo().getToken();
      if (token != null || Condition.loginFirebase == true) {
        Navigator.of(context, rootNavigator: true).push(
          CustomNavigator(
            child: CartPage(),
          ),
        );
      } else {
        String teks = 'dapat mengakses keranjang';
        return _showDialogNotLogin(teks);
      }
    }

    handleNotLoginNotif() async {
      var token = await UsersInfo().getToken();
      if (token != null || Condition.loginFirebase == true) {
        Navigator.of(context, rootNavigator: true).push(
          CustomNavigator(
            child: Notifikasi(),
          ),
        );
      } else {
        String teks = 'dapat mengakses notifikasi';
        return _showDialogNotLogin(teks);
      }
    }

    return AnimatedContainer(
      duration: Duration(milliseconds: 500),
      curve: Curves.ease,
      color: color,
      width: SizeConfig.screenWidth,
      height: getProportionateScreenHeight(40),
      child: Padding(
        padding: EdgeInsets.only(
          right: getProportionateScreenWidth(10),
          left: getProportionateScreenWidth(5),
        ),
        child: Row(
          children: [
            IconButton(
              alignment: Alignment.centerLeft,
              icon: Theme.of(context).brightness == Brightness.dark
                  ? SvgPicture.asset('assets/icons/arrow_back_dark.svg')
                  : SvgPicture.asset('assets/icons/arrow_back.svg'),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            Spacer(),
            Transform.scale(
              origin: Offset(-11, 0),
              scale: getProportionateScreenHeight(1),
              child: Container(
                padding: EdgeInsets.fromLTRB(getProportionateScreenHeight(3), 0,
                    getProportionateScreenHeight(4), 0),
                child: Consumer<NotificationProvider>(
                  builder: (context, value, child) {
                    return IconBtnWithCounter(
                      icon: Theme.of(context).brightness == Brightness.dark
                          ? SvgPicture.asset(
                              "assets/icons/notification_dark.svg")
                          : SvgPicture.asset("assets/icons/notification.svg"),
                      numOfitem:
                          (Condition.loginEmail || Condition.loginFirebase)
                              ? value.notificationCount
                              : 0,
                      press: () {
                        handleNotLoginNotif();
                      },
                    );
                  },
                ),
              ),
            ),
            !Condition.loginEmail && !Condition.loginFirebase
                ? Transform.scale(
                    origin: Offset(0, 0),
                    scale: getProportionateScreenHeight(1),
                    child: Container(
                      padding: EdgeInsets.fromLTRB(
                          getProportionateScreenHeight(3),
                          0,
                          getProportionateScreenHeight(3),
                          0),
                      child: IconBtnWithCounter(
                        numOfitem: 0,
                        icon: Theme.of(context).brightness == Brightness.dark
                            ? SvgPicture.asset("assets/icons/cart_dark.svg")
                            : SvgPicture.asset("assets/icons/cart.svg"),
                        press: () => handleNotLoginCart(),
                      ),
                    ),
                  )
                : Transform.scale(
                    origin: Offset(0, 0),
                    scale: getProportionateScreenHeight(1),
                    child: Container(
                      padding: EdgeInsets.fromLTRB(
                          getProportionateScreenHeight(3),
                          0,
                          getProportionateScreenHeight(3),
                          0),
                      child: Consumer<CartsProvider>(
                        builder: (context, state, _) {
                          return IconBtnWithCounter(
                            numOfitem: state.result == null ? 0 : state.lenght,
                            icon: Theme.of(context).brightness ==
                                    Brightness.dark
                                ? SvgPicture.asset("assets/icons/cart_dark.svg")
                                : SvgPicture.asset("assets/icons/cart.svg"),
                            press: () => handleNotLoginCart(),
                          );
                        },
                      ),
                    ),
                  ),
          ],
        ),
      ),
    );
  }
}
