import 'dart:async';

import 'package:flutter/material.dart';
import 'package:gymlink_module_web/components/app_bar.dart';
import 'package:gymlink_module_web/components/heading.dart';
import 'package:gymlink_module_web/components/history_item_card.dart';
import 'package:gymlink_module_web/tools/relative.dart';
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';

List<Map<String, String>> orders = [
  {"image": "product.png", "price": "120", "id": "66", "date": "11.09.2001"},
  {
    "image": "product.png",
    "price": "150",
    "id": "56",
    "date": "11.09.2001",
  },
  {
    "image": "product.png",
    "price": "250",
    "id": "98",
    "date": "11.09.2001",
  },
  {
    "image": "product.png",
    "price": "300",
    "id": "50",
    "date": "11.09.2001",
  },
  {
    "image": "product.png",
    "price": "100",
    "id": "30",
    "date": "11.09.2001",
  }
];

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

  @override
  State<HistoryPage> createState() => _HistoryPageState();
}

class _HistoryPageState extends State<HistoryPage> {
  List<Map<String, String>> my_orders = [];
  late Timer _updateTimer;

  @override
  void initState() {
    super.initState();
    my_orders = orders;
    ordersRefresh();
  }

  void ordersRefresh() {
    _updateTimer = Timer.periodic(const Duration(minutes: 1), _onRefresh);
  }

  void _onLoad() async {
    await Future.delayed(const Duration(milliseconds: 1000));
    setState(() {
      my_orders.add(
        {
          "image": "product.png",
          "price": "120",
          "id": "666666",
          "date": "11.09.2001"
        },
      );
    });
  }

  @override
  void dispose() {
    _updateTimer.cancel();
    super.dispose();
  }

  Future<void> _onRefresh(Timer timer) async {
    await Future.delayed(const Duration(milliseconds: 1000));
    debugPrint('refreshed');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const GymLinkAppBar(),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          const GymLinkHeader(title: 'История заказов'),
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: LazyLoadScrollView(
                    onEndOfPage: _onLoad,
                    scrollOffset: 200,
                    child: RefreshIndicator(
                      edgeOffset: 55,
                      onRefresh: () => _onRefresh(_updateTimer),
                      child: Stack(
                        children: [
                          ListView.builder(
                            itemCount: my_orders.length,
                            itemBuilder: (context, index) {
                              final item = my_orders[index];
                              return HistoryItemCard(
                                id: item['id']!,
                                cost: item['price']!,
                                date: item['date']!,
                                image: Image(
                                  image: AssetImage('assets/${item['image']!}'),
                                  width: 50,
                                ),
                                status: OrderStatus.completed,
                              );
                            },
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
                getSpacer(context: context)
              ],
            ),
          ),
        ],
      ),
    );
  }
}
