import 'package:core_localization/core_localization.dart';
import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_modular/flutter_modular.dart';
import '../blocs/experience_bloc.dart';
import '../widgets/experience_custom_input.dart';
import '../widgets/experience_section_title.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => Modular.get<ExperienceBloc>()..add(ExperienceLoaded()),
      child: const _ExperienceView(),
    );
  }
}

class _ExperienceView extends StatelessWidget {
  const _ExperienceView();

  String _getIndustryLabel(dynamic node, String key) {
    switch (key) {
      case 'hospitality': return node.hospitality;
      case 'food_service': return node.food_service;
      case 'warehouse': return node.warehouse;
      case 'events': return node.events;
      case 'retail': return node.retail;
      case 'healthcare': return node.healthcare;
      case 'other': return node.other;
      default: return key;
    }
  }

  String _getSkillLabel(dynamic node, String key) {
    switch (key) {
      case 'food_service': return node.food_service;
      case 'bartending': return node.bartending;
      case 'event_setup': return node.event_setup;
      case 'hospitality': return node.hospitality;
      case 'warehouse': return node.warehouse;
      case 'customer_service': return node.customer_service;
      case 'cleaning': return node.cleaning;
      case 'security': return node.security;
      case 'retail': return node.retail;
      case 'cooking': return node.cooking;
      case 'cashier': return node.cashier;
      case 'server': return node.server;
      case 'barista': return node.barista;
      case 'host_hostess': return node.host_hostess;
      case 'busser': return node.busser;
      default: return key;
    }
  }

  @override
  Widget build(BuildContext context) {
    final i18n = t.staff.onboarding.experience;

    return Scaffold(
      backgroundColor: UiColors.background,
      appBar: UiAppBar(
        title: i18n.title,
        onLeadingPressed: () => Modular.to.pop(),
      ),
      body: BlocConsumer<ExperienceBloc, ExperienceState>(
        listener: (context, state) {
          if (state.status == ExperienceStatus.success) {
            Modular.to.pop();
          }
        },
        builder: (context, state) {
          return Column(
            children: [
              Expanded(
                child: SingleChildScrollView(
                  padding: EdgeInsets.all(UiConstants.space5),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      ExperienceSectionTitle(title: i18n.industries_title),
                      Text(
                        i18n.industries_subtitle,
                        style: UiTypography.body2m.copyWith(color: UiColors.textSecondary),
                      ),
                      SizedBox(height: UiConstants.space3),
                      Wrap(
                        spacing: UiConstants.space2,
                        runSpacing: UiConstants.space2,
                        children: state.availableIndustries
                            .map(
                              (i) => UiChip(
                                label: _getIndustryLabel(i18n.industries, i),
                                isSelected: state.selectedIndustries.contains(i),
                                onTap: () => BlocProvider.of<ExperienceBloc>(context)
                                    .add(ExperienceIndustryToggled(i)),
                                variant: state.selectedIndustries.contains(i)
                                    ? UiChipVariant.primary
                                    : UiChipVariant.secondary,
                              ),
                            )
                            .toList(),
                      ),
                      SizedBox(height: UiConstants.space6),
                      ExperienceSectionTitle(title: i18n.skills_title),
                      Text(
                        i18n.skills_subtitle,
                        style: UiTypography.body2m.copyWith(color: UiColors.textSecondary),
                      ),
                      SizedBox(height: UiConstants.space3),
                      Wrap(
                        spacing: UiConstants.space2,
                        runSpacing: UiConstants.space2,
                        children: state.availableSkills
                            .map(
                              (s) => UiChip(
                                label: _getSkillLabel(i18n.skills, s),
                                isSelected: state.selectedSkills.contains(s),
                                onTap: () => BlocProvider.of<ExperienceBloc>(context)
                                    .add(ExperienceSkillToggled(s)),
                                variant: state.selectedSkills.contains(s)
                                    ? UiChipVariant.primary
                                    : UiChipVariant.secondary,
                              ),
                            )
                            .toList(),
                      ),
                      SizedBox(height: UiConstants.space4),
                      const ExperienceCustomInput(),
                      SizedBox(height: UiConstants.space4),
                      _buildCustomSkillsList(state, i18n),
                      SizedBox(height: UiConstants.space10),
                    ],
                  ),
                ),
              ),
              _buildSaveButton(context, state, i18n),
            ],
          );
        },
      ),
    );
  }

  Widget _buildCustomSkillsList(ExperienceState state, dynamic i18n) {
    final customSkills = state.selectedSkills
        .where((s) => !state.availableSkills.contains(s))
        .toList();
    if (customSkills.isEmpty) return const SizedBox.shrink();

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          i18n.custom_skills_title,
          style: UiTypography.body2m.copyWith(color: UiColors.textSecondary),
        ),
        SizedBox(height: UiConstants.space2),
        Wrap(
          spacing: UiConstants.space2,
          runSpacing: UiConstants.space2,
          children: customSkills.map((skill) {
            return UiChip(
              label: skill,
              variant: UiChipVariant.accent,
            );
          }).toList(),
        ),
      ],
    );
  }

  Widget _buildSaveButton(BuildContext context, ExperienceState state, dynamic i18n) {
    return Container(
      padding: EdgeInsets.all(UiConstants.space4),
      decoration: BoxDecoration(
        color: UiColors.bgPopup,
        border: Border(top: BorderSide(color: UiColors.border)),
      ),
      child: SafeArea(
        child: SizedBox(
          width: double.infinity,
          child: UiButton.primary(
            onPressed: state.status == ExperienceStatus.loading
                ? null
                : () => BlocProvider.of<ExperienceBloc>(context).add(ExperienceSubmitted()),
            fullWidth: true,
            text: state.status == ExperienceStatus.loading ? null : i18n.save_button,
            child: state.status == ExperienceStatus.loading
                ? SizedBox(
                    height: 20.0,
                    width: 20.0,
                    child: CircularProgressIndicator(
                      strokeWidth: 2,
                      valueColor: AlwaysStoppedAnimation<Color>(UiColors.white), // UiColors.primaryForeground is white mostly
                    ),
                  )
                : null,
          ),
        ),
      ),
    );
  }
}
