import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';
import 'package:yimaru_app/ui/common/app_colors.dart';
import 'package:yimaru_app/ui/views/onboarding/onboarding_viewmodel.dart';
import 'package:yimaru_app/ui/widgets/language_button.dart';

class OnboardingAppBar extends ViewModelWidget<OnboardingViewModel> {
  final bool language;
  final bool showBackButton;
  final bool showLanguageSelection;

  const OnboardingAppBar(
      {super.key,
      this.language = false,
        this.showBackButton = true,
        this.showLanguageSelection = true});

  @override
  Widget build(BuildContext context, OnboardingViewModel viewModel) =>
      _buildAppBarWrapper(viewModel);

  Widget _buildAppBarWrapper(OnboardingViewModel viewModel) => Container(
        height: 125,
        width: double.maxFinite,
        alignment: Alignment.bottomCenter,
        decoration: const BoxDecoration(
          color: kcPrimaryColor,
          borderRadius: BorderRadius.only(
            bottomLeft: Radius.circular(24),
            bottomRight: Radius.circular(24),
          ),
        ),
        padding: const EdgeInsets.only(bottom: 25, right: 15),
        child: _buildAppBarItems(viewModel),
      );

  Widget _buildAppBarItems(OnboardingViewModel viewModel) => Stack(
        children: _buildAppBarItemChildren(viewModel),
      );

  List<Widget> _buildAppBarItemChildren(OnboardingViewModel viewModel) => [
        if (showBackButton) _buildBackButtonWrapper(viewModel),
        _buildRightButton(viewModel)
      ];

  Widget _buildBackButtonWrapper(OnboardingViewModel viewModel) => Align(
        alignment: Alignment.bottomLeft,
        child: _buildBackButton(viewModel),
      );

  Widget _buildBackButton(OnboardingViewModel viewModel) => BackButton(
        onPressed: ()=> viewModel.pop(language: language),
        style: const ButtonStyle(
            foregroundColor: WidgetStatePropertyAll(kcWhiteColor)),
      );

  Widget _buildRightButton(OnboardingViewModel viewModel) => Align(
      alignment: Alignment.bottomRight,
      child: showLanguageSelection
          ? _buildLanguageSelector(viewModel)
          : _buildCloseButton());

  Widget _buildLanguageSelector(OnboardingViewModel viewModel) =>
      LanguageButton(
        language: 'EN',
        onTap: () => viewModel.next(page: 23),
      );

  Widget _buildCloseButton() => IconButton(
        onPressed: () {},
        icon: _buildCloseIcon(),
      );

  Widget _buildCloseIcon() => const Icon(
        Icons.close,
        color: kcWhiteColor,
      );
}
