import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:slrpg_app/l10n/app_localizations.dart';

import '../../../../core/theme/app_theme.dart';
import '../../../../core/constants/app_constants.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'bodyweight_input_screen.g.dart';

// Provider to store onboarding data
@Riverpod(keepAlive: true)
class OnboardingData extends _$OnboardingData {
  @override
  Map<String, dynamic> build() => {};

  void update(Map<String, dynamic> Function(Map<String, dynamic> state) cb) {
    final newData = cb(state);
    state = {...state, ...newData};
  }

  void updateData(Map<String, dynamic> newValue) {
    state = {...state, ...newValue};
  }

  void clear() {
    state = {};
  }

  void removeKey(String key) {
    final newState = Map<String, dynamic>.from(state);
    newState.remove(key);
    state = newState;
  }
}

class BodyweightInputScreen extends ConsumerStatefulWidget {
  const BodyweightInputScreen({super.key});

  @override
  ConsumerState<BodyweightInputScreen> createState() =>
      _BodyweightInputScreenState();
}

class _BodyweightInputScreenState extends ConsumerState<BodyweightInputScreen> {
  double _bodyweight = 80.0;
  bool _useKg = true;

  void _handleContinue() {
    // Store bodyweight
    ref.read(onboardingDataProvider.notifier).updateData({
      'bodyweight': _bodyweight,
    });

    context.go('/onboarding/strength-test');
  }

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    return Scaffold(
      appBar: AppBar(
        title: Text(l10n.setupProfileTitle),
        // title: const Text('Setup Profile'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => context.go('/onboarding/welcome'),
        ),
      ),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(24),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              // Progress Indicator
              LinearProgressIndicator(
                value: 0.25,
                backgroundColor: AppTheme.xpBarBackground,
                color: AppTheme.primaryColor,
              ),
              const SizedBox(height: 32),

              // Title
              Text(
                l10n.bodyweightTitle,
                // 'What\'s your current bodyweight?',
                style: Theme.of(context).textTheme.displayMedium,
              ),
              const SizedBox(height: 16),
              Text(
                l10n.bodyweightSubtitle,
                // 'We need this to calculate your weighted calisthenics exercises',
                style: Theme.of(context).textTheme.bodyMedium,
              ),
              const SizedBox(height: 48),

              // Unit Toggle
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SegmentedButton<bool>(
                    segments: [
                      ButtonSegment(value: true, label: Text(l10n.unitKg)),
                      ButtonSegment(value: false, label: Text(l10n.unitLbs)),
                    ],
                    selected: {_useKg},
                    onSelectionChanged: (Set<bool> newSelection) {
                      setState(() {
                        _useKg = newSelection.first;
                        if (_useKg) {
                          _bodyweight = _bodyweight / 2.20462;
                        } else {
                          _bodyweight = _bodyweight * 2.20462;
                        }
                      });
                    },
                  ),
                ],
              ),
              const SizedBox(height: 32),

              // Bodyweight Display
              Center(
                child: Column(
                  children: [
                    Text(
                      _bodyweight.toStringAsFixed(1),
                      style: Theme.of(context).textTheme.displayLarge?.copyWith(
                            fontSize: 72,
                            color: AppTheme.primaryColor,
                          ),
                    ),
                    Text(
                      _useKg ? 'kg' : 'lbs',
                      style: Theme.of(context).textTheme.headlineMedium,
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 32),

              // Slider
              Slider(
                value: _bodyweight,
                min: _useKg
                    ? AppConstants.minBodyweight
                    : AppConstants.minBodyweight * 2.20462,
                max: _useKg
                    ? AppConstants.maxBodyweight
                    : AppConstants.maxBodyweight * 2.20462,
                divisions: _useKg ? 160 : 352,
                activeColor: AppTheme.primaryColor,
                onChanged: (value) {
                  setState(() => _bodyweight = value);
                },
              ),

              const Spacer(),

              // Continue Button
              ElevatedButton(
                onPressed: _handleContinue,
                child: Text(l10n.continueButton),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
