import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../domain/entities/tour.dart';
import '../../blocs/scan/scan_bloc.dart';
import '../scan/scan_page.dart';

/// FSA Page - Fahrscheinautomat
/// Entspricht Lua: ShowFsaScreen + CreateLoadingFsaView
class FsaPage extends StatefulWidget {
  final Tour tour;

  const FsaPage({
    super.key,
    required this.tour,
  });

  @override
  State<FsaPage> createState() => _FsaPageState();
}

class _FsaPageState extends State<FsaPage> {
  @override
  void initState() {
    super.initState();
    context.read<ScanBloc>().add(InitializeScan(widget.tour));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(widget.tour.locationName ?? 'Fahrscheinautomat'),
            const Text(
              'Objekt-Einbuchung',
              style: TextStyle(fontSize: 14, color: Colors.white70),
            ),
          ],
        ),
        actions: [
          IconButton(
            icon: const Icon(Icons.qr_code_scanner),
            onPressed: () => _openScanner(context),
          ),
        ],
      ),
      body: Column(
        children: [
          // Header
          _buildHeader(context),
          
          // FSA-spezifische Info
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                children: [
                  _buildFsaInfo(context),
                  _buildObjectTypes(context),
                ],
              ),
            ),
          ),
          
          // Scan Button
          _buildScanButton(context),
        ],
      ),
    );
  }

  Widget _buildHeader(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16),
      color: const Color(0xFFA4D4F0),
      child: Row(
        children: [
          Container(
            width: 80,
            height: 80,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
            ),
            child: const Icon(
              Icons.confirmation_number,
              size: 48,
              color: Colors.blue,
            ),
          ),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  'Fahrscheinautomat',
                  style: Theme.of(context).textTheme.titleLarge?.copyWith(
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  widget.tour.locationName ?? 'FSA',
                  style: Theme.of(context).textTheme.bodyMedium,
                ),
                if (widget.tour.remark != null)
                  Text(
                    widget.tour.remark!,
                    style: Theme.of(context).textTheme.bodySmall,
                  ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildFsaInfo(BuildContext context) {
    return Container(
      margin: const EdgeInsets.all(16),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.blue.shade50,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: Colors.blue.shade200),
      ),
      child: const Column(
        children: [
          Text(
            'Gültige Objekte:',
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 16,
            ),
          ),
          SizedBox(height: 8),
          Text(
            '• GK (Geldkassette): station → in_fa → ret_fail\n'
            '• HP (Hauptkasse): station ↔ in_fa (Wechsel)\n'
            '• FR (Fahrkartenrolle): station ↔ in_fa',
            style: TextStyle(fontSize: 14),
          ),
        ],
      ),
    );
  }

  Widget _buildObjectTypes(BuildContext context) {
    final objectTypes = [
      _ObjectTypeInfo('Geldkassette (GK)', 'MEK, BEK', Icons.money, Colors.green),
      _ObjectTypeInfo('Hauptkasse (HP)', 'H1, H2, H3', Icons.print, Colors.blue),
      _ObjectTypeInfo('Fahrkartenrolle (FR)', 'P', Icons.receipt, Colors.orange),
    ];

    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            'Objekttypen',
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 12),
          ...objectTypes.map((type) => _buildObjectTypeCard(context, type)),
        ],
      ),
    );
  }

  Widget _buildObjectTypeCard(BuildContext context, _ObjectTypeInfo type) {
    return Container(
      margin: const EdgeInsets.only(bottom: 8),
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: Colors.grey.shade100,
        borderRadius: BorderRadius.circular(8),
      ),
      child: Row(
        children: [
          Container(
            padding: const EdgeInsets.all(8),
            decoration: BoxDecoration(
              color: type.color.withValues(alpha: 26),
              borderRadius: BorderRadius.circular(8),
            ),
            child: Icon(type.icon, color: type.color),
          ),
          const SizedBox(width: 12),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  type.name,
                  style: const TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 15,
                  ),
                ),
                Text(
                  type.subtypes,
                  style: TextStyle(
                    fontSize: 13,
                    color: Colors.grey.shade600,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildScanButton(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16),
      child: ElevatedButton.icon(
        onPressed: () => _openScanner(context),
        icon: const Icon(Icons.qr_code_scanner),
        label: const Text('Barcode scannen'),
        style: ElevatedButton.styleFrom(
          minimumSize: const Size(double.infinity, 56),
        ),
      ),
    );
  }

  void _openScanner(BuildContext context) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (_) => ScanPage(tour: widget.tour),
      ),
    );
  }
}

class _ObjectTypeInfo {
  final String name;
  final String subtypes;
  final IconData icon;
  final Color color;

  _ObjectTypeInfo(this.name, this.subtypes, this.icon, this.color);
}
