import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

import '../../../common/router/app_router.dart';
import '../../../common/theme/app_text.dart';
import '../../../common/theme/spacing.dart';

class DebugPage extends StatelessWidget {
  const DebugPage({super.key});

  @override
  Widget build(BuildContext context) {
    final cs = ShadTheme.of(context).colorScheme;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: cs.background,
        surfaceTintColor: Colors.transparent,
        elevation: 0,
        scrolledUnderElevation: 0,
        title: Text(
          '调试工具',
          style: TextStyle(
            fontSize: 17,
            fontWeight: FontWeight.w700,
            color: cs.foreground,
          ),
        ),
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios_new, color: cs.foreground, size: 18),
          onPressed: () => context.pop(),
        ),
      ),
      body: ListView(
        padding: const EdgeInsets.fromLTRB(Spacing.page, 8, Spacing.page, 24),
        children: [
          _Section(
            title: '状态',
            child: _Card(
              child: Text(
                kReleaseMode ? 'release' : 'debug',
                style: AppText.body.copyWith(color: cs.mutedForeground),
              ),
            ),
          ),
          _Section(
            title: '入口',
            child: _Card(
              children: [
                _Row(
                  icon: Icons.data_object_outlined,
                  title: '请求',
                  subtitle: '查看 Dio 请求和响应',
                  onTap: () => context.push(AppRoutes.fancyDioInspector),
                ),
                _Row(
                  icon: Icons.info_outline,
                  title: '应用信息',
                  subtitle: '版本、包名和构建信息',
                  onTap: () => context.push(AppRoutes.debugInfo),
                ),
                _Row(
                  icon: Icons.text_fields,
                  title: 'Typography',
                  subtitle: '查看字号和文本样式基线',
                  onTap: () => context.push(AppRoutes.debugTypography),
                ),
                _Row(
                  icon: Icons.system_update_alt_outlined,
                  title: 'Shorebird',
                  subtitle: '查看补丁与更新状态',
                  onTap: () => context.push(AppRoutes.debugShorebird),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class _Section extends StatelessWidget {
  const _Section({required this.title, required this.child});

  final String title;
  final Widget child;

  @override
  Widget build(BuildContext context) {
    final cs = ShadTheme.of(context).colorScheme;
    return Padding(
      padding: const EdgeInsets.only(bottom: Spacing.sectionGap),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 4, bottom: 10),
            child: Text(
              title,
              style: AppText.meta.copyWith(
                fontSize: 13,
                fontWeight: FontWeight.w600,
                color: cs.mutedForeground,
              ),
            ),
          ),
          child,
        ],
      ),
    );
  }
}

class _Card extends StatelessWidget {
  const _Card({this.child, this.children = const []});

  final Widget? child;
  final List<Widget> children;

  @override
  Widget build(BuildContext context) {
    final cs = ShadTheme.of(context).colorScheme;
    return Container(
      decoration: BoxDecoration(
        color: cs.card,
        border: Border.all(color: cs.border),
        borderRadius: BorderRadius.circular(Spacing.radiusBase),
      ),
      clipBehavior: Clip.antiAlias,
      child:
          child ??
          Column(
            children: [
              for (var i = 0; i < children.length; i++) ...[
                if (i > 0) Divider(height: 1, thickness: 1, color: cs.border),
                children[i],
              ],
            ],
          ),
    );
  }
}

class _Row extends StatelessWidget {
  const _Row({
    required this.icon,
    required this.title,
    required this.subtitle,
    required this.onTap,
  });

  final IconData icon;
  final String title;
  final String subtitle;
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    final cs = ShadTheme.of(context).colorScheme;
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: onTap,
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 15),
          child: Row(
            children: [
              Icon(icon, size: 20, color: cs.foreground),
              const SizedBox(width: 13),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      title,
                      style: AppText.body.copyWith(
                        color: cs.foreground,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                    const SizedBox(height: 4),
                    Text(
                      subtitle,
                      style: AppText.meta.copyWith(
                        color: cs.mutedForeground,
                        fontSize: 12.5,
                      ),
                    ),
                  ],
                ),
              ),
              Icon(Icons.chevron_right, size: 18, color: cs.mutedForeground),
            ],
          ),
        ),
      ),
    );
  }
}
