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 'package:krow_domain/krow_domain.dart';

import '../blocs/experience_bloc.dart';
import '../widgets/experience_section_title.dart';

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

  String _getIndustryLabel(dynamic node, Industry industry) {
    switch (industry) {
      case Industry.hospitality: return node.hospitality;
      case Industry.foodService: return node.food_service;
      case Industry.warehouse: return node.warehouse;
      case Industry.events: return node.events;
      case Industry.retail: return node.retail;
      case Industry.healthcare: return node.healthcare;
      case Industry.other: return node.other;
    }
  }

  String _getSkillLabel(dynamic node, ExperienceSkill skill) {
    switch (skill) {
      case ExperienceSkill.foodService: return node.food_service;
      case ExperienceSkill.bartending: return node.bartending;
      case ExperienceSkill.eventSetup: return node.event_setup;
      case ExperienceSkill.hospitality: return node.hospitality;
      case ExperienceSkill.warehouse: return node.warehouse;
      case ExperienceSkill.customerService: return node.customer_service;
      case ExperienceSkill.cleaning: return node.cleaning;
      case ExperienceSkill.security: return node.security;
      case ExperienceSkill.retail: return node.retail;
      case ExperienceSkill.driving: return node.driving;
      case ExperienceSkill.cooking: return node.cooking;
      case ExperienceSkill.cashier: return node.cashier;
      case ExperienceSkill.server: return node.server;
      case ExperienceSkill.barista: return node.barista;
      case ExperienceSkill.hostHostess: return node.host_hostess;
      case ExperienceSkill.busser: return node.busser;
    }
  }

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

    return Scaffold(
      appBar: UiAppBar(
        title: i18n.title,
        onLeadingPressed: () => Modular.to.pop(),
      ),
      body: BlocProvider<ExperienceBloc>(
        create: (context) => Modular.get<ExperienceBloc>(),
        child: BlocConsumer<ExperienceBloc, ExperienceState>(
          listener: (context, state) {
          if (state.status == ExperienceStatus.success) {
            ScaffoldMessenger.of(context).showSnackBar(
              const SnackBar(content: Text('Experience saved successfully')),
            );
            Modular.to.pop();
          } else if (state.status == ExperienceStatus.failure) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text(state.errorMessage ?? 'An error occurred')),
            );
          }
        },
          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.value),
                                  onTap: () => BlocProvider.of<ExperienceBloc>(context)
                                      .add(ExperienceSkillToggled(s.value)),
                                  variant: state.selectedSkills.contains(s.value)
                                      ? UiChipVariant.primary
                                      : UiChipVariant.secondary,
                                ),
                              )
                              .toList(),
                        ),
                      ],
                    ),
                  ), 
                ),
                _buildSaveButton(context, state, i18n),
              ],
            );
          },
        ),
      ),
    );
  }

  Widget _buildCustomSkillsList(ExperienceState state, dynamic i18n) {
    final customSkills = state.selectedSkills
        .where((s) => !state.availableSkills.any((e) => e.value == 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: 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,
        ),
      ),
    );
  }
}
