import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

  @override
  State<AboutScreen> createState() => _AboutScreenState();
}

class _AboutScreenState extends State<AboutScreen> {
  static const _nativeChannel = MethodChannel('com.example.watch/launcher');

  // --- متغیرهای مربوط به بخش توضیحات (برای تنظیمات) ---
  int _descTapCount = 0;
  DateTime? _descLastTapTime;
  bool _descIsHolding = false;

  // --- متغیرهای مربوط به بخش نسخه (برای EngineerMode) ---
  int _verTapCount = 0;
  DateTime? _verLastTapTime;
  bool _verIsHolding = false;

  // تابع باز کردن تنظیمات ساعت
  Future<void> _openWatchSettings() async {
    try {
      await _nativeChannel.invokeMethod('openWatchSettings');
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('خطا در باز کردن تنظیمات')),
        );
      }
    }
  }

  // تابع باز کردن EngineerMode
  Future<void> _openEngineerMode() async {
    try {
      await _nativeChannel.invokeMethod('openEngineerMode');
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(
          context,
        ).showSnackBar(const SnackBar(content: Text('خطا در باز کردن مهندسی')));
      }
    }
  }

  // --- هندلرهای بخش توضیحات ---
  void _handleDescTap() {
    if (_descIsHolding) return;
    final now = DateTime.now();
    if (_descLastTapTime != null &&
        now.difference(_descLastTapTime!) > const Duration(seconds: 1)) {
      _descTapCount = 0;
    }
    _descTapCount++;
    _descLastTapTime = now;
    if (_descTapCount == 5) HapticFeedback.lightImpact();
  }

  void _handleDescLongPressStart(LongPressStartDetails details) {
    setState(() => _descIsHolding = true);
    if (_descTapCount >= 5) {
      HapticFeedback.heavyImpact();
      _openWatchSettings();
    } else {
      _descTapCount = 0;
    }
  }

  void _handleDescLongPressEnd(LongPressEndDetails details) {
    setState(() => _descIsHolding = false);
  }

  // --- هندلرهای بخش نسخه ---
  void _handleVerTap() {
    if (_verIsHolding) return;
    final now = DateTime.now();
    if (_verLastTapTime != null &&
        now.difference(_verLastTapTime!) > const Duration(seconds: 1)) {
      _verTapCount = 0;
    }
    _verTapCount++;
    _verLastTapTime = now;
    if (_verTapCount == 5) HapticFeedback.lightImpact();
  }

  void _handleVerLongPressStart(LongPressStartDetails details) {
    setState(() => _verIsHolding = true);
    if (_verTapCount >= 5) {
      HapticFeedback.heavyImpact();
      _openEngineerMode();
    } else {
      _verTapCount = 0;
    }
  }

  void _handleVerLongPressEnd(LongPressEndDetails details) {
    setState(() => _verIsHolding = false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF000000),
      body: SafeArea(
        child: Center(
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                // --- لوگو ---
                Container(
                  width: 100,
                  height: 100,
                  decoration: const BoxDecoration(
                    color: Colors.white,
                    shape: BoxShape.circle,
                  ),
                  child: Padding(
                    padding: const EdgeInsets.all(15.0),
                    child: Image.asset(
                      'assets/images/logo.png',
                      errorBuilder: (context, error, stackTrace) {
                        return const Icon(
                          Icons.error_outline,
                          color: Colors.grey,
                          size: 50,
                        );
                      },
                    ),
                  ),
                ),
                const SizedBox(height: 30),

                // --- بخش توضیحات سازنده (قابل کلیک برای تنظیمات) ---
                GestureDetector(
                  onTap: _handleDescTap,
                  onLongPressStart: _handleDescLongPressStart,
                  onLongPressEnd: _handleDescLongPressEnd,
                  behavior: HitTestBehavior.opaque,
                  child: Container(
                    margin: const EdgeInsets.symmetric(horizontal: 20),
                    padding: const EdgeInsets.all(15),
                    decoration: BoxDecoration(
                      color: const Color(0xFF1C1C1E),
                      borderRadius: BorderRadius.circular(15),
                      border: Border.all(color: Colors.white.withOpacity(0.1)),
                    ),
                    child: const Text(
                      'توسعه یافته توسط تیم هوش مصنوعی و فناوری های نو ظهور سپاه ثارلله استان کرمان',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 14,
                        height: 1.5,
                      ),
                    ),
                  ),
                ),
                const SizedBox(height: 20),

                // --- بخش نسخه (قابل کلیک برای EngineerMode) ---
                GestureDetector(
                  onTap: _handleVerTap,
                  onLongPressStart: _handleVerLongPressStart,
                  onLongPressEnd: _handleVerLongPressEnd,
                  behavior: HitTestBehavior.opaque,
                  child: Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 20,
                      vertical: 10,
                    ),
                    decoration: BoxDecoration(
                      color: Colors.white.withOpacity(0.05),
                      borderRadius: BorderRadius.circular(20),
                      border: Border.all(color: Colors.white.withOpacity(0.1)),
                    ),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: const [
                        Icon(
                          Icons.info_outline,
                          color: Colors.blueAccent,
                          size: 18,
                        ),
                        SizedBox(width: 8),
                        Text(
                          'نسخه ۱.۰.۰',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 16,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),

                const SizedBox(height: 40),

                IconButton(
                  onPressed: () => Navigator.pop(context),
                  icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
