import 'package:food_delivery_client/feature/account/presentation/mixins/account_mixins.dart';

import '../../../../../../food_delivery_client.dart';

class WAccountBody extends StatelessWidget with AccountMixins {
  WAccountBody({super.key});

  @override
  Widget build(BuildContext context) {
    final List<String> titles = [
      context.loc.orders,
      context.loc.yourFavourites,
      context.loc.restaurantRewards,
      context.loc.wallet,
      context.loc.sendAGift,
      context.loc.help,
      context.loc.promotions,
      context.loc.uberPass,
      context.loc.deliverWithUber,
      context.loc.changeLanguage,
      context.loc.settings,
    ];
    return WLayout(
      child: Scaffold(
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(56),
          child: WAccountAppBar().paddingSymmetric(horizontal: 19),
        ),
        body: LayoutBuilder(
          builder: (context, constraints) => ConstrainedBox(
            constraints: BoxConstraints(minHeight: constraints.maxHeight),
            child: ListView.builder(
              shrinkWrap: true,
              itemCount: leadingIcons.length,
              padding: EdgeInsets.symmetric(vertical:5),
              physics: const AlwaysScrollableScrollPhysics(),
              scrollDirection: Axis.vertical,
              itemBuilder: (context, index) => WAccountRowItem(
                onTap: () {
                  if (index == 9) {
                    WChangeLanguage().show(context);
                  }
                },
                svgPath: leadingIcons[index],
                title: titles[index],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return AppBar(
      centerTitle: false,
      leading: ClipRRect(
        borderRadius: AppUtils.kBorderRadius40,
        child: SizedBox(
          child: Image.asset(
            AppImages.imgAvatar,
            height: 36,
            width: 36,
            fit: BoxFit.cover,
          ),
        ),
      ).paddingAll(8),
      title: Text('Felix', style: AppTextStyles.size18Medium),
    );
  }
}

class WAccountRowItem extends StatelessWidget {
  const WAccountRowItem({
    super.key,
    required this.svgPath,
    required this.title,
    this.subTitle,
    this.leadingIcon,
    required this.onTap,
  });

  final String svgPath;
  final String title;
  final String? subTitle;
  final Widget? leadingIcon;
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Ink(
        child: Row(
          spacing: 24,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            leadingIcon ??
                SizedBox(
                  height: 24,
                  width: 24,
                  child: SvgPicture.asset(svgPath),
                ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(title, style: AppTextStyles.size14Medium),
                if (subTitle != null)
                  Text(
                    subTitle!,
                    style: AppTextStyles.size14Medium.copyWith(
                      color: AppColors.c34A853,
                    ),
                  ),
              ],
            ),
          ],
        ).paddingSymmetric(vertical: 16, horizontal: 22),
      ),
    );
  }
}

class WChangeLanguage extends StatelessWidget with AccountMixins {
  WChangeLanguage({super.key});

  show(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (context) => Wrap(children: [this]),
    );
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<LanguageBloc, LanguageState>(
      builder: (context, state) {
        return Material(
          color: AppColors.cFFFFFF,
          borderRadius: AppUtils.kBorderRadiusTop20,
          child: SizedBox(
            width: context.w,
            child: SafeArea(
              child: Column(
                children: [
                  10.verticalSpace,
                  SizedBox(
                    height: 6,
                    width: 100,
                    child: DecoratedBox(
                      decoration: BoxDecoration(
                        color: AppColors.cEEEEEE,
                        borderRadius: AppUtils.kBorderRadius8,
                      ),
                    ),
                  ),
                  10.verticalSpace,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        context.loc.changeLanguage,
                        style: AppTextStyles.size20Medium,
                      ),
                      IconButton(
                        onPressed: () {
                          context.pop();
                        },
                        icon: SvgPicture.asset(AppIcons.icClose),
                      ),
                    ],
                  ).paddingSymmetric(horizontal: 16),
                  15.verticalSpace,
                  Column(
                    children: List.generate(3, (index) {
                      return AppListTile(
                        onPressed: () {
                          context.read<LanguageBloc>().add(
                            LanguageEvent.changed(L10n.locales[index]),
                          );
                        },
                        isSelected: L10n.locales[index] == state.currentLocale,
                        svgPath: '',
                        leading: SizedBox(
                          height: 24,
                          width: 24,
                          child: Image.asset(flags[index]),
                        ),
                        title: languages[index],
                        titleTextStyle: AppTextStyles.size14Medium,
                      );
                    }),
                  ),
                  10.verticalSpace
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
