import 'package:flutter/material.dart';
import '../models/onboarding_model.dart';
import 'widgets/glass_card.dart';

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

  @override
  State<OnboardingPage> createState() => _OnboardingPageState();
}

class _OnboardingPageState extends State<OnboardingPage>
    with SingleTickerProviderStateMixin {
  int currentIndex = 0;

  final List<OnboardingModel> onboardingData = [
    OnboardingModel(
      image: 'assets/onboarding/bg1.jpg',
      title: 'What we do',
      description:
          'Easily manage and promote your attractions while connecting with travelers and handling bookings in one place.',
    ),
    OnboardingModel(
      image: 'assets/onboarding/bg2.png',
      title: 'Manage Booking Seamlessly',
      description:
          'Track and manage all your bookings in real-time from one easy-to-use dashboard.',
    ),
  ];

  late AnimationController _controller;
  late Animation<double> _fadeAnimation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 800),
    );
    _fadeAnimation = Tween<double>(begin: 1.0, end: 0.0).animate(_controller);
  }

  void _nextPage() async {
    await _controller.forward();
    setState(() {
      currentIndex = (currentIndex + 1) % onboardingData.length;
    });
    _controller.reverse();
  }

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

  @override
  Widget build(BuildContext context) {
    final current = onboardingData[currentIndex];

    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: [
          FadeTransition(
            opacity: _fadeAnimation.drive(Tween(begin: 0.1, end: 1.0)),
            child: Image.asset(current.image, fit: BoxFit.cover),
          ),
          current.title == "Manage Booking Seamlessly"
              ? Container()
              : SafeArea(
                  child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Align(
                      alignment: Alignment.topRight,
                      child: OutlinedButton(
                        onPressed: () {},
                        style: OutlinedButton.styleFrom(
                          side: const BorderSide(color: Colors.white),
                          foregroundColor: Colors.white,
                          padding: const EdgeInsets.symmetric(
                            horizontal: 20,
                            vertical: 8,
                          ),
                        ),
                        child: const Text("Skip"),
                      ),
                    ),
                  ),
                ),
          Align(
            alignment: Alignment.bottomCenter,
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: GlassCard(
                title: current.title,
                description: current.description,
                currentIndex: currentIndex,
                total: onboardingData.length,
                onContinue: _nextPage,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
