import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_pos_ecommerce/core/components/menu_button.dart';
import 'package:flutter_pos_ecommerce/core/constants/global_variable.dart';
import 'package:flutter_pos_ecommerce/core/extensions/int_ext.dart';
import 'package:flutter_pos_ecommerce/core/extensions/string_ext.dart';
import 'package:flutter_pos_ecommerce/module/ecommerce/data/datasources/auth_local_datasource.dart';
import 'package:flutter_pos_ecommerce/module/pos/presentation/setting/dialogs/discount_dialog.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart' as http;
import '../../../../../core/assets/assets.gen.dart';
import '../../../../../core/components/buttons.dart';
import '../../../../../core/components/spaces.dart';
import '../../../../../core/router/app_router.dart';
import '../../home/bloc/checkout/checkout_bloc.dart';
import '../widgets/cart_tile.dart';
import 'package:badges/badges.dart' as badges;

class CartPage extends StatefulWidget {
  const CartPage({super.key});

  @override
  State<CartPage> createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {
  bool showDiscountButton = false;

  @override
  void initState() {
    super.initState();
    _checkTransactionCount();
  }

  Future<void> _checkTransactionCount() async {
    final authData = await AuthLocalDatasource().getAuthData();
    final response = await http.get(
      Uri.parse('${GlobalVariable.baseUrl}/api/transaction-count'),
      headers: {
        'Authorization': 'Bearer ${authData!.accessToken}',
        'Accept': 'application/json',
      },
    );

    if (response.statusCode == 200) {
      final data = jsonDecode(response.body);
      setState(() {
        showDiscountButton = data['transaction_count'] > 10;
      });
    } else {
      print('Failed to load transaction count');
    }
  }

  @override
  Widget build(BuildContext context) {
    // final List<ProductModel> carts = [
    //   ProductModel(
    //     images: [
    //       Assets.images.products.earphone.path,
    //       Assets.images.products.earphone.path,
    //       Assets.images.products.earphone.path,
    //     ],
    //     name: 'Earphone',
    //     price: 320000,
    //     stock: 20,
    //     description:
    //         'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ',
    //     store: StoreModel(
    //       name: 'CWB Online Store',
    //       type: StoreEnum.officialStore,
    //       imageUrl: 'https://avatars.githubusercontent.com/u/534678?v=4',
    //     ),
    //   ),
    //   ProductModel(
    //     images: [
    //       Assets.images.products.sepatu.path,
    //       Assets.images.products.sepatu2.path,
    //       Assets.images.products.sepatu.path,
    //     ],
    //     name: 'Sepatu Nike',
    //     price: 2200000,
    //     stock: 20,
    //     description:
    //         'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ',
    //     store: StoreModel(
    //       name: 'CWB Online Store',
    //       type: StoreEnum.officialStore,
    //       imageUrl: 'https://avatars.githubusercontent.com/u/534678?v=4',
    //     ),
    //   ),
    // ];

    return Scaffold(
      appBar: AppBar(
        title: const Text('Keranjang'),
        actions: [
          BlocBuilder<CheckoutBloc, CheckoutState>(
            builder: (context, state) {
              return state.maybeWhen(
                loaded: (checkout, discount, _, __, ___, ____, ______) {
                  final totalQuantity = checkout.fold<int>(
                    0,
                    (previousValue, element) =>
                        previousValue + element.quantity,
                  );
                  return totalQuantity > 0
                      ? badges.Badge(
                          badgeContent: Text(
                            totalQuantity.toString(),
                            style: const TextStyle(color: Colors.white),
                          ),
                          child: IconButton(
                            onPressed: () {
                              context.goNamed(
                                RouteConstants.cart,
                                pathParameters: PathParameters().toMap(),
                              );
                            },
                            icon: Assets.icons.cart.svg(height: 24.0),
                          ),
                        )
                      : IconButton(
                          onPressed: () {
                            context.goNamed(
                              RouteConstants.cart,
                              pathParameters: PathParameters().toMap(),
                            );
                          },
                          icon: Assets.icons.cart.svg(height: 24.0),
                        );
                },
                orElse: () => const SizedBox.shrink(),
              );
            },
          ),
          const SizedBox(
            width: 16,
          )
        ],
      ),
      body: ListView(
        padding: const EdgeInsets.all(20.0),
        children: [
          BlocBuilder<CheckoutBloc, CheckoutState>(
            builder: (context, state) {
              return state.maybeWhen(
                  orElse: () => const SizedBox.shrink(),
                  loaded: (checkout, discount, _, __, ___, ____, _____) {
                    return ListView.separated(
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      itemCount: checkout.length,
                      itemBuilder: (context, index) => CartTile(
                        data: checkout[index],
                      ),
                      separatorBuilder: (context, index) =>
                          const SpaceHeight(16.0),
                    );
                  });
            },
          ),
          const SpaceHeight(50.0),
          if (showDiscountButton)
            MenuButton(
              iconPath: Assets.icons.qrCode.path,
              label: 'Diskon',
              onPressed: () {
                showDialog(
                  context: context,
                  barrierDismissible: false,
                  builder: (context) => const DiscountDialog(),
                );
              },
            ),
          const SpaceHeight(50.0),
          Row(
            children: [
              const Text(
                'Total',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                ),
              ),
              const Spacer(),
              BlocBuilder<CheckoutBloc, CheckoutState>(
                builder: (context, state) {
                  final price = state.maybeWhen(
                    orElse: () => 0,
                    loaded: (products, discount, _, __, ___, ____, _____) =>
                        products.fold(
                      0,
                      (previousValue, element) =>
                          previousValue +
                          (element.product.price! * element.quantity),
                    ),
                  );
                  final discount = state.maybeWhen(
                      orElse: () => 0,
                      loaded: (products, discount, _, __, ___, ____, _____) {
                        if (discount == null) {
                          return 0;
                        }
                        return discount.value!
                            .replaceAll('.00', '')
                            .toIntegerFromText;
                      });
                  // final total = state.maybeWhen(
                  //   orElse: () => 0,
                  //   loaded: (checkout, discount, _, __, ___, ____, _____) {
                  //     return checkout.fold<int>(
                  //       0,
                  //       (previousValue, element) =>
                  //           previousValue +
                  //           (element.quantity * element.product.price!),
                  //     );
                  //   },
                  // );
                  print('price $price');
                  print('discount $discount');
                  final totalBayar = price - (discount / 100 * price);
                  print('totalBayar $totalBayar');
                  return Text(
                    totalBayar.currencyFormatRp,
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                    ),
                  );
                },
              ),
            ],
          ),
          const SpaceHeight(40.0),
          BlocBuilder<CheckoutBloc, CheckoutState>(
            builder: (context, state) {
              final totalQty = state.maybeWhen(
                orElse: () => 0,
                loaded: (checkout, _, __, ___, ____, _____, ______) {
                  return checkout.fold<int>(
                    0,
                    (previousValue, element) =>
                        previousValue + element.quantity,
                  );
                },
              );
              return Button.filled(
                onPressed: () async {
                  final isAuth = await AuthLocalDatasource().isAuth();
                  if (!isAuth) {
                    context.pushNamed(
                      RouteConstants.login,
                    );
                  } else {
                    context.goNamed(
                      RouteConstants.address,
                      pathParameters: PathParameters(
                        rootTab: RootTab.order,
                      ).toMap(),
                    );
                  }
                },
                label: 'Checkout ($totalQty)',
              );
              // return Button.filled(
              //   onPressed: () {
              //     context.goNamed(
              //       RouteConstants.address,
              //       pathParameters: PathParameters(
              //         rootTab: RootTab.order,
              //       ).toMap(),
              //     );
              //   },
              //   label: 'Checkout (10)',
              // );
            },
          ),
        ],
      ),
    );
  }
}
