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';

/// GI Page - Geldinstitut
/// Entspricht Lua: ShowGiScreen + CreateLoadingGiView
class GiPage extends StatefulWidget {
  final Tour tour;

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

  @override
  State<GiPage> createState() => _GiPageState();
}

class _GiPageState extends State<GiPage> {
  @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 ?? 'Geldinstitut'),
            const Text(
              'Übergabe',
              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),
          
          // GI-spezifische Info
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                children: [
                  _buildGiInfo(context),
                  _buildExpectedObjects(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.account_balance,
              size: 48,
              color: Colors.blue,
            ),
          ),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  'Geldinstitut',
                  style: Theme.of(context).textTheme.titleLarge?.copyWith(
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  widget.tour.locationName ?? 'Bank',
                  style: Theme.of(context).textTheme.bodyMedium,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildGiInfo(BuildContext context) {
    return Container(
      margin: const EdgeInsets.all(16),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.green.shade50,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: Colors.green.shade200),
      ),
      child: const Column(
        children: [
          Text(
            'Erwartete Objekte:',
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 16,
            ),
          ),
          SizedBox(height: 8),
          Text(
            '• GK mit Status "ret_gi_fzg" → "fin_gi"\n'
            '• SB (Safebag) mit Status "ret_gi_fzg" → "fin_gi"\n'
            '• Leere Kassetten: "unknown" → "ret_ds_empty"',
            style: TextStyle(fontSize: 14),
          ),
        ],
      ),
    );
  }

  Widget _buildExpectedObjects(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            'Zu übergebende Objekte',
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 12),
          
          // Geldkassetten
          _buildObjectStatusCard(
            context,
            'Geldkassetten (GK)',
            'ret_gi_fzg → fin_gi',
            Icons.money,
            Colors.green,
          ),
          
          // Safebags
          _buildObjectStatusCard(
            context,
            'Safebags (SB)',
            'ret_gi_fzg → fin_gi',
            Icons.shopping_bag,
            Colors.blue,
          ),
          
          // Leere Kassetten
          _buildObjectStatusCard(
            context,
            'Leere Kassetten',
            'unknown → ret_ds_empty',
            Icons.remove_circle_outline,
            Colors.orange,
          ),
        ],
      ),
    );
  }

  Widget _buildObjectStatusCard(
    BuildContext context,
    String title,
    String transition,
    IconData icon,
    Color color,
  ) {
    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: color.withValues(alpha: 26),
              borderRadius: BorderRadius.circular(8),
            ),
            child: Icon(icon, color: color),
          ),
          const SizedBox(width: 12),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: const TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 15,
                  ),
                ),
                Text(
                  transition,
                  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),
      ),
    );
  }
}
