import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:kmobile/data/models/user.dart';
import 'package:material_symbols_icons/material_symbols_icons.dart';
import '../../../l10n/app_localizations.dart';

class CustomerInfoScreen extends StatefulWidget {
  final User user;
  const CustomerInfoScreen({super.key, required this.user});

  @override
  State<CustomerInfoScreen> createState() => _CustomerInfoScreenState();
}

class _CustomerInfoScreenState extends State<CustomerInfoScreen> {
  late final User user = widget.user;
  int _selectedCard = 0; // 0 for Personal Info, 1 for KYC

  String _maskPrimaryId(String? primaryId) {
    if (primaryId == null || primaryId.length <= 4) {
      return primaryId ?? 'N/A';
    }
    final lastFour = primaryId.substring(primaryId.length - 4);
    return '*' * (primaryId.length - 4) + lastFour;
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Scaffold(
        appBar: AppBar(
          title: Text(
            AppLocalizations.of(context)
                .customerInfo
                .replaceFirst(RegExp('\n'), ''),
          ),
        ),
        body: SafeArea(
          child: Stack(
            children: [
              SingleChildScrollView(
                physics: const AlwaysScrollableScrollPhysics(),
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    children: [
                      Card(
                        elevation: 0,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(12),
                          side: BorderSide(
                            color: theme.colorScheme.outline.withOpacity(0.2),
                            width: 1,
                          ),
                        ),
                        child: Padding(
                          padding: const EdgeInsets.all(16.0),
                          child: Row(
                            children: [
                              const SizedBox(
                                width: 56,
                                height: 56,
                                child: CircleAvatar(
                                  radius: 50,
                                  child: Icon(
                                    Symbols.person,
                                    size: 56,
                                  ),
                                ),
                              ),
                              const SizedBox(width: 12),
                              // Name + mobile
                              Expanded(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      // If you want to show the user's name instead, replace below.
                                      user.name ?? '',
                                      style:
                                          theme.textTheme.titleLarge?.copyWith(
                                        fontWeight: FontWeight.w600,
                                      ),
                                    ),
                                    const SizedBox(height: 4),
                                    Text(
                                      user.cifNumber ?? '',
                                      style:
                                          theme.textTheme.bodyMedium?.copyWith(
                                        color: theme.colorScheme.onSurface
                                            .withOpacity(0.7),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      const SizedBox(height: 16),
                      // Toggle Buttons for Personal Info and KYC
                      SizedBox(
                        width: double.infinity,
                        child: CupertinoSlidingSegmentedControl<int>(
                          groupValue: _selectedCard,
                          thumbColor: Theme.of(context)
                              .colorScheme
                              .onPrimary, // Set selected switch color to theme primary color
                          onValueChanged: (int? newValue) {
                            if (newValue != null) {
                              setState(() {
                                _selectedCard = newValue;
                              });
                            }
                          },
                          children: {
                            0: Padding(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 20, vertical: 10),
                              child: Text(
                                  AppLocalizations.of(context).personaldetails),
                            ),
                            1: Padding(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 20, vertical: 10),
                              child:
                                  Text(AppLocalizations.of(context).kycdetails),
                            ),
                          },
                        ),
                      ),
                      const SizedBox(height: 16),
                      // Card that shows content based on the toggle
                      Card(
                        elevation: 0,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(12),
                          side: BorderSide(
                            color: theme.colorScheme.outline.withOpacity(0.2),
                            width: 1,
                          ),
                        ),
                        child: Padding(
                          padding: const EdgeInsets.all(16.0),
                          child: AnimatedSwitcher(
                            duration: const Duration(milliseconds: 300),
                            child: _selectedCard == 0
                                ? _buildPersonalInfo(theme)
                                : _buildKycDetails(theme),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              IgnorePointer(
                child: Center(
                  child: Opacity(
                    opacity: 0.07, // Reduced opacity
                    child: ClipOval(
                      child: Image.asset(
                        'assets/images/logo.png',
                        width: 200, // Adjust size as needed
                        height: 200, // Adjust size as needed
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ));
  }

  Widget _buildPersonalInfo(ThemeData theme) {
    return Column(
      key: const ValueKey('personal_info'),
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          AppLocalizations.of(context).personaldetails,
          style: theme.textTheme.titleMedium?.copyWith(
            fontWeight: FontWeight.w600,
          ),
        ),
        const SizedBox(height: 16),
        InfoField(
          label: AppLocalizations.of(context).activeAccounts,
          value: user.activeAccounts?.toString() ?? 'N/A',
        ),
        InfoField(
          label: AppLocalizations.of(context).mobileNumber,
          value: user.mobileNo ?? 'N/A',
        ),
        InfoField(
          label: AppLocalizations.of(context).dateOfBirth,
          value: (user.dateOfBirth != null && user.dateOfBirth!.length == 8)
              ? '${user.dateOfBirth!.substring(0, 2)}-${user.dateOfBirth!.substring(2, 4)}-${user.dateOfBirth!.substring(4, 8)}'
              : 'N/A',
        ),
        InfoField(
          label: AppLocalizations.of(context).branchCode,
          value: user.branchId ?? 'N/A',
        ),
        InfoField(
          label: AppLocalizations.of(context).address,
          value: user.address ?? 'N/A',
        ),
      ],
    );
  }

  Widget _buildKycDetails(ThemeData theme) {
    return Column(
      key: const ValueKey('kyc_details'),
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          AppLocalizations.of(context).kycdetails,
          style: theme.textTheme.titleMedium?.copyWith(
            fontWeight: FontWeight.w600,
          ),
        ),
        const SizedBox(height: 16),
        InfoField(
          label: AppLocalizations.of(context).primaryId,
          value: _maskPrimaryId(user.primaryId),
        ),
      ],
    );
  }
}

class InfoField extends StatelessWidget {
  final String label;
  final String value;

  const InfoField({super.key, required this.label, required this.value});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Container(
      width: double.infinity,
      margin: const EdgeInsets.symmetric(vertical: 8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            label,
            style: theme.textTheme.bodySmall?.copyWith(
              color: theme.colorScheme.onSurface.withOpacity(0.6),
            ),
          ),
          const SizedBox(height: 4),
          Text(
            value.isEmpty ? 'N/A' : value,
            style: theme.textTheme.titleMedium?.copyWith(
              fontWeight: FontWeight.w600,
            ),
          ),
        ],
      ),
    );
  }
}
