// ignore_for_file: unused_local_variable, deprecated_member_use

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:lba/data/model/workingHours.dart';
import 'package:lba/gen/assets.gen.dart';
import 'package:lba/res/colors.dart';
import 'package:lba/widgets/customCard.dart';
import 'package:lba/widgets/dividerTitle.dart';
import 'package:lba/widgets/openChecker.dart';
import 'package:lba/widgets/rate.dart';
import 'package:maps_launcher/maps_launcher.dart';

class Shop extends StatefulWidget {
  final String shopName;
  final double star;
  final String place;
  final List<String> branches;
  final List<WorkingHours> workingHours;
  final String facilities;

  const Shop({
    super.key,
    required this.shopName,
    required this.star,
    required this.place,
    required this.branches,
    required this.workingHours,
    required this.facilities,
  });

  @override
  State<Shop> createState() => _ShopState();
}

class _ShopState extends State<Shop> with TickerProviderStateMixin {
  late String selectedPlace;
  bool isActiveOffer = true;

  late AnimationController _staggeredController;
  late List<Animation<double>> _staggeredAnimations;


  @override
  void initState() {
    super.initState();
    selectedPlace = widget.place;

    _staggeredController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 1200),
    );

    final int itemCount = 8;
    _staggeredAnimations = List.generate(itemCount, (index) {
      return Tween<double>(begin: 0.0, end: 1.0).animate(
        CurvedAnimation(
          parent: _staggeredController,
          curve: Interval(
            (0.1 * index),
            (0.5 + 0.1 * index).clamp(0.0, 1.0),
            curve: Curves.easeOutCubic,
          ),
        ),
      );
    });

    _staggeredController.forward();
  }

  @override
  void dispose() {
    _staggeredController.dispose();
    super.dispose();
  }

  Widget _buildAnimatedWidget(Widget child, int index) {
    return FadeTransition(
      opacity: _staggeredAnimations[index],
      child: SlideTransition(
        position: Tween<Offset>(
          begin: const Offset(0.0, 0.5),
          end: Offset.zero,
        ).animate(_staggeredAnimations[index]),
        child: child,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final now = DateTime.now();
    final schedule = IsOpenChecker.getTodaySchedule(widget.workingHours);
    final isOpen = IsOpenChecker.isOpenNow(widget.workingHours);

    String timeRange = '';
    if (isOpen && schedule.shifts.isNotEmpty) {
      for (final shift in schedule.shifts) {
        timeRange +=
            '${formatTime12Hour(shift.openAt)} - ${formatTime12Hour(shift.closeAt)}  ';
      }
    }

    return SingleChildScrollView(
      child: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildAnimatedWidget(
              Row(
                children: [
                  SvgPicture.asset(
                    Assets.icons.shop.path,
                    color: Colors.black,
                    height: 25,
                  ),
                  SizedBox(width: 10),
                  Text(
                    widget.shopName,
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 17),
                  ),
                ],
              ),
              0,
            ),
            SizedBox(height: 15),
            _buildAnimatedWidget(
              Row(
                children: [
                  CustomStarRating(rating: widget.star),
                  SizedBox(width: 5),
                  Text(
                    widget.star.toString(),
                    style: TextStyle(color: LightAppColors.productDetailDivider),
                  ),
                ],
              ),
              1,
            ),
            SizedBox(height: 5),
            _buildAnimatedWidget(
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      SvgPicture.asset(
                        Assets.icons.location.path,
                        color: Colors.black,
                      ),
                      SizedBox(width: 5),
                      Text(
                        selectedPlace,
                        style: TextStyle(
                          color: LightAppColors.productDetailDivider,
                        ),
                      ),
                    ],
                  ),
                  PopupMenuButton<String>(
                    color: Colors.white,
                    icon: SvgPicture.asset(
                      Assets.icons.arrowDown.path,
                      color: Colors.black,
                      height: 20,
                    ),
                    onSelected: (String newPlace) {
                      setState(() {
                        selectedPlace = newPlace;
                      });
                    },
                    itemBuilder: (BuildContext context) {
                      return widget.branches.map((String branch) {
                        return PopupMenuItem<String>(
                          value: branch,
                          child: Text(branch),
                        );
                      }).toList();
                    },
                  ),
                ],
              ),
              2,
            ),
            SizedBox(height: 5),
            _buildAnimatedWidget(
              Row(
                children: [
                  SvgPicture.asset(Assets.icons.clock.path),
                  SizedBox(width: 5),
                  Text(
                    isOpen ? 'Open Now' : 'Closed',
                    style: TextStyle(color: isOpen ? Colors.green : Colors.red),
                  ),
                  SizedBox(width: 5),
                  Container(
                    color: LightAppColors.productDetailDivider,
                    width: 1,
                    height: 13,
                  ),
                  SizedBox(width: 5),
                  Text(
                    timeRange,
                    style: TextStyle(color: LightAppColors.productDetailDivider),
                  ),
                ],
              ),
              3,
            ),
            SizedBox(height: 15),
            _buildAnimatedWidget(DividerTitleWidget(title: "Facilities"), 4),
            _buildAnimatedWidget(
              SizedBox(
                height: 50,
                child: ListView.builder(
                  physics: NeverScrollableScrollPhysics(),
                  itemCount: 1,
                  itemBuilder: (context, index) {
                    return Row(
                      children: [
                        SvgPicture.asset(Assets.icons.tick.path),
                        SizedBox(width: 5),
                        Text(
                          widget.facilities,
                          style: TextStyle(
                            color: LightAppColors.productDetailDivider,
                          ),
                        ),
                      ],
                    );
                  },
                ),
              ),
              5,
            ),
            SizedBox(height: 20),
            _buildAnimatedWidget(
              ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: LightAppColors.confirmButton,
                  minimumSize: const Size(double.infinity, 48),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(15),
                  ),
                ),
                onPressed: () {
                  MapsLauncher.launchCoordinates(25.2399, 55.2744);
                },
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SvgPicture.asset(
                      Assets.icons.routing2.path,
                      color: Colors.black,
                    ),
                    SizedBox(width: 5),
                    Text(
                      "Direction",
                      style: const TextStyle(
                        color: Colors.black,
                        fontSize: 16,
                      ),
                    ),
                  ],
                ),
              ),
              6,
            ),
            SizedBox(height: 30),
            _buildAnimatedWidget(
              Text(
                "List of offers",
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 18,
                ),
              ),
              7,
            ),
            SizedBox(height: 15),
            Container(
              height: 45,
              width: 150,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: const Color.fromARGB(17, 77, 77, 77),
              ),
              child: Row(
                children: [
                  GestureDetector(
                    onTap: () {
                      setState(() {
                        isActiveOffer = true;
                      });
                    },
                    child: Padding(
                      padding: const EdgeInsets.all(5.0),
                      child: Container(
                        height: 35,
                        width: 70,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(10),
                          color: isActiveOffer ? Colors.white : Colors.transparent,
                        ),
                        child: Center(child: Text("Active")),
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: () {
                      setState(() {
                        isActiveOffer = false;
                      });
                    },
                    child: Padding(
                      padding: const EdgeInsets.all(5.0),
                      child: Container(
                        height: 35,
                        width: 60,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(10),
                          color: !isActiveOffer ? Colors.white : Colors.transparent,
                        ),
                        child: Center(child: Text("Old")),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(height: 15),
            SizedBox(
              height: 160, 
              child: ListView.builder(
                scrollDirection: Axis.horizontal,
                shrinkWrap: true,
                itemCount: 2,
                itemBuilder: (context, index) {
                  return CustomCard(
                    title: "Calamaro Table Lamp",
                    description: "Elegant lighting with a modern twist.",
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}