import 'package:autosos_flutter/router/app_router.dart';
import 'package:autosos_flutter/util/sp_util.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

/// 新手引导页（对应 Android WelcomeActivity + ViewPager）
class WelcomePage extends StatefulWidget {
  const WelcomePage({super.key});

  @override
  State<WelcomePage> createState() => _WelcomePageState();
}

class _WelcomePageState extends State<WelcomePage> {
  final _controller = PageController();
  int _index = 0;

  final _pages = const [
    _WelcomeItem(
      img: 'images/4.0x/welcome_1.png',
      title: '快速接单',
      subtitle: '实时推送附近订单，第一时间响应',
    ),
    _WelcomeItem(
      img: 'images/4.0x/welcome_2.png',
      title: '拍照取证',
      subtitle: '现场拍照、客户签字，全流程电子化',
    ),
    _WelcomeItem(
      img: 'images/4.0x/welcome_3.png',
      title: '收入透明',
      subtitle: '订单金额、提现记录一目了然',
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          PageView.builder(
            controller: _controller,
            onPageChanged: (i) => setState(() => _index = i),
            itemCount: _pages.length,
            itemBuilder: (_, i) => _pages[i],
          ),
          Positioned(
            bottom: 40,
            left: 0,
            right: 0,
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: List.generate(
                    _pages.length,
                    (i) => AnimatedContainer(
                      duration: const Duration(milliseconds: 200),
                      margin: const EdgeInsets.symmetric(horizontal: 4),
                      width: _index == i ? 24 : 8,
                      height: 8,
                      decoration: BoxDecoration(
                        color: _index == i
                            ? Theme.of(context).primaryColor
                            : Colors.grey.shade400,
                        borderRadius: BorderRadius.circular(4),
                      ),
                    ),
                  ),
                ),
                const SizedBox(height: 24),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 32),
                  child: SizedBox(
                    width: double.infinity,
                    height: 48,
                    child: ElevatedButton(
                      onPressed: _index == _pages.length - 1
                          ? _finish
                          : () => _controller.nextPage(
                                duration: const Duration(milliseconds: 200),
                                curve: Curves.easeIn,
                              ),
                      child: Text(_index == _pages.length - 1 ? '立即体验' : '下一步'),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Future<void> _finish() async {
    SPUtil().setBool('isWelcome', true);
    if (!mounted) return;
    context.go(Routes.home);
  }
}

class _WelcomeItem extends StatelessWidget {
  final String img;
  final String title;
  final String subtitle;

  const _WelcomeItem({required this.img, required this.title, required this.subtitle});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 24),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(img, fit: BoxFit.contain, height: 280),
          const SizedBox(height: 32),
          Text(title,
              style: const TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
          const SizedBox(height: 12),
          Text(subtitle,
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 14, color: Colors.grey.shade600)),
        ],
      ),
    );
  }
}