import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nearle/views/helpers/constants/Colorconstants.dart';
import 'package:nearle/views/introscreens/intro1.dart';
import 'package:nearle/views/introscreens/intro2.dart';
import 'package:nearle/views/introscreens/intro3.dart';
import 'package:nearle/views/onboardscreens/Sign_in.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';

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

  @override
  State<Introscreen> createState() => _IntroscreenState();
}

class _IntroscreenState extends State<Introscreen> {
  late final PageController _controller;
  static const String _prefsHasSeenIntroKey = 'has_seen_intro';

  void _finishOnboarding() {
    _completeOnboarding();
  }

  Future<void> _completeOnboarding() async {
    try {
      final prefs = await SharedPreferences.getInstance();
      await prefs.setBool(_prefsHasSeenIntroKey, true);
    } catch (_) {}

    if (!mounted) return;
    Get.offAll(() => const SignIn());
  }

  @override
  void initState() {
    super.initState();
    _controller = PageController();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        alignment: Alignment.bottomCenter, // positions the indicator
        children: [
          PageView(
            controller: _controller,
            children: [
              Intro1(),
              Intro2(),
              Intro3(controller: _controller, onFinish: _finishOnboarding),
            ],
          ),
          Container(
            // Move indicator slightly up so it doesn't sit under the bottom curve
            alignment: const Alignment(0, 0.55),
            child: SmoothPageIndicator(
              controller: _controller,
              count: 3,
              effect: ExpandingDotsEffect(
                expansionFactor: 3, // How much the active dot expands
                dotHeight: 7,
                dotWidth: 7,
                spacing: 8,
                dotColor: Colors.grey,
                activeDotColor: ColorConstants.primaryColor,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
