import 'package:comwell_key_app/force_update/force_update_cubit.dart';
import 'package:comwell_key_app/themes/light_theme.dart';
import 'package:comwell_key_app/utils/l10n_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'dart:io' show Platform;

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

  @override
  Widget build(BuildContext context) {
    final textTheme = Theme.of(context).textTheme;
    return BlocProvider(
      create: (_) => ForceUpdateCubit(),
      child: BlocConsumer<ForceUpdateCubit, ForceUpdateState>(
        listener: (context, state) {
          if (state.error != null) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text(state.error!)),
            );
          }
        },
        builder: (context, state) {
          return Scaffold(
            extendBody: true,
            extendBodyBehindAppBar: true,
            body: Container(
              constraints: const BoxConstraints.expand(),
              decoration: const BoxDecoration(
                image: DecorationImage(
                  image:
                      AssetImage('assets/images/login_screen_background.png'),
                  fit: BoxFit.cover,
                ),
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  // Logo
                  Expanded(
                    flex: 3,
                    child: Image.asset('assets/images/Logo.png',
                        width: 175, height: 50),
                  ),
                  const SizedBox(height: 32),
                  // Force Update Message
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 40.0),
                    child: Column(
                      children: [
                        Text(
                          context.strings.force_update_title,
                          textAlign: TextAlign.center,
                          style: textTheme.headlineMedium
                              ?.copyWith(color: Colors.white),
                        ),
                        const SizedBox(height: 8),
                        Text(
                          context.strings.force_update_description,
                          textAlign: TextAlign.center,
                          style: textTheme.bodySmall?.copyWith(
                              color: Colors.white.withValues(alpha: 0.65)),
                        ),
                      ],
                    ),
                  ),
                  const SizedBox(height: 36),
                  // Update Button
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 32.0),
                    child: ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.white,
                        textStyle: textTheme.headlineSmall
                            ?.copyWith(color: colorTertiary),
                      ),
                      onPressed: state.isLoading
                          ? null
                          : () {
                              if (Platform.isAndroid) {
                                context
                                    .read<ForceUpdateCubit>()
                                    .startAndroidUpdate();
                              } else if (Platform.isIOS) {
                                context.read<ForceUpdateCubit>().openAppStore();
                              }
                            },
                      child: state.isLoading
                          ? const SizedBox(
                              width: 24,
                              height: 24,
                              child: CircularProgressIndicator(strokeWidth: 2),
                            )
                          : Text(context.strings.force_update_button),
                    ),
                  ),
                  SizedBox(height: 60 + MediaQuery.of(context).padding.bottom),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}