import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:gymlink_module_web/components/app_bar.dart';
import 'package:gymlink_module_web/components/heading.dart';
import 'package:gymlink_module_web/components/order_confirm_item_card.dart';
import 'package:gymlink_module_web/tools/prefs.dart';

List<Map<String, dynamic>> cart = [
  {
    "name": "Протеин",
    "image": "product.png",
    "price": "120",
    "details": "Test details",
    "id": "34fa3126-bfaf-5dec-8f4a-b246c097ef73"
  },
  {
    "name": "Протеин",
    "image": "product.png",
    "price": "150",
    "details": "Test details",
    "id": "34a26e82-7656-5e98-a44a-c2d01d0b1ad1123"
  },
  {
    "name": "Протеин",
    "image": "product.png",
    "price": "250",
    "details": "Test details",
    "id": "4fb204b7-3f9e-52a2-bed1-415c00a31a37123"
  },
  {
    "name": "Протеин",
    "image": "product.png",
    "price": "300",
    "details": "Test details",
    "id": "09b2f5bb-683e-5c39-ae89-b8e152fa8bcf123"
  },
  {
    "name": "Протеин",
    "image": "product.png",
    "price": "100",
    "details": "Test details",
    "id": "cd1b6817-db94-5394-be1d-af88af79749f123"
  }
];

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

  @override
  State<OrderConfirmationPage> createState() => _OrderConfirmationPageState();
}

class _OrderConfirmationPageState extends State<OrderConfirmationPage> {
  List<Map<String, dynamic>> cartItems = [];
  int totalPrice = 0;

  @override
  void initState() {
    super.initState();
    getCart().then((value) {
      setState(() {
        cartItems = value.map((element) {
          final item = cart.firstWhere((e) => e['id'] == element['id']);
          return {...item, 'count': element['count'] as int};
        }).toList();
        totalPrice = cartItems.fold(
            0,
            (sum, item) =>
                sum + int.parse(item['price']) * item['count'] as int);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const GymLinkAppBar(),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const GymLinkHeader(title: 'Оформление заказа'),
          const MarkdownBody(data: '## Состав заказа:'),
          Expanded(
            child: ConstrainedBox(
              constraints: const BoxConstraints(maxHeight: 350),
              child: ListView.builder(
                itemCount: cartItems.length,
                itemBuilder: (context, index) {
                  final item = cartItems[index];
                  return OrderConfirmItemCard(
                    name: item['name'],
                    image: Image(
                        image: AssetImage('assets/${item['image']}'),
                        width: 50,
                        height: 50),
                    price: double.parse(item['price']),
                    count: item['count'],
                  );
                },
              ),
            ),
          ),
          const SizedBox(
            height: 10,
          ),
          Expanded(
            child: Column(
              children: [
                MarkdownBody(data: '## Итого: $totalPrice'),
                Expanded(
                  child: TextField(
                    decoration: InputDecoration(
                      hintText: 'Адрес доставки',
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(10),
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: TextField(
                    decoration: InputDecoration(
                      hintText: 'Получатель',
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(10),
                      ),
                    ),
                  ),
                ),
                ElevatedButton(
                  onPressed: () {
                    print('debugprint');
                    // if (kIsWeb) {
                    //   Navigator.of(context).push(
                    //     MaterialPageRoute(
                    //       builder: (context) => const OrderPayPage(),
                    //     ),
                    //   );
                    // } else {
                    //   debugPrint('test');
                    // }
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Theme.of(context).primaryColor,
                    shape: const RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(50)),
                    ),
                    foregroundColor: Colors.white,
                  ),
                  child: const Text('Оформить заказ'),
                ),
                const SizedBox(height: 20),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
