import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../application/language/language_bloc.dart';
import '../../../common/extension/extension.dart';
import '../../../common/theme/theme.dart';
import '../../../infrastructure/language/language.dart';
import 'widgets/language_tile.dart';

@RoutePage()
class LanguagePage extends StatelessWidget {
  const LanguagePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.background,
      appBar: AppBar(
        title: Text(context.lang.select_language),
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back, color: Colors.white),
          onPressed: () => context.router.back(),
        ),
      ),
      body: BlocBuilder<LanguageBloc, LanguageState>(
        builder: (context, state) {
          return ListView.builder(
            padding: const EdgeInsets.all(16),
            itemCount: languages.length,
            itemBuilder: (context, index) {
              final language = languages[index];
              final isSelected =
                  state.language.locale.languageCode ==
                      language.locale.languageCode
                  ? true
                  : false;

              return LanguageTile(
                isSelected: isSelected,
                language: language,
                onTap: () => context.read<LanguageBloc>().add(
                  LanguageEvent.changeLanguage(language),
                ),
              );
            },
          );
        },
      ),
    );
  }
}
