import 'package:flutter/material.dart';
import 'package:food_health/api/dto/profile_options_dto.dart';

import '../data/state.dart';
import '../util/common.dart';
import 'common.dart';

class DietaryPreferences extends StatefulWidget {
  final List<ProfileOptionDto> options;

  const DietaryPreferences({super.key, required this.options});

  @override
  State<DietaryPreferences> createState() => _DietaryPreferencesState();
}

class _DietaryPreferencesState extends State<DietaryPreferences> {
  ///切换标签
  void _handToggle(String tag) {
    var state = SelectionProvider.of(context);
    if (getIsSelect(tag)) {
      state.update((p) => p.dietaryPreferencesList.remove(tag));
    } else {
      state.update((p) => p.dietaryPreferencesList.add(tag));
    }
  }

  ///选中年龄
  void _handAgeRange(String tag) {
    var state = SelectionProvider.of(context);
    state.update((p) => p.ageRange = tag);
  }

  ///等级
  void _handActivityLevel(String tag) {
    var state = SelectionProvider.of(context);
    state.update((p) => p.activityLevel = tag);
  }

  ///是否标签选中
  bool getIsSelect(String tag) {
    var state = SelectionProvider.of(context);
    return state.userProfile.dietaryPreferencesList.contains(tag);
  }

  @override
  Widget build(BuildContext context) {
    var state = SelectionProvider.of(context);
    return StepContentCard(
      children: [
        CardTitle(title: "Dietary Restrictions & Preferences"),
        Container(
          margin: EdgeInsets.only(bottom: 15),
          child: OptionList(
            options: getOptions(widget.options, "dietary_restrictions"),
            selects: state.userProfile.dietaryPreferencesList,
            onTap: _handToggle,
          ),
        ),
        CardTitle(title: "Age Range"),
        Container(
          margin: EdgeInsets.only(bottom: 15),
          child: RadioGroup(
            options: getOptions(widget.options, "age_ranges"),
            value: state.userProfile.ageRange,
            onChanged: _handAgeRange,
          ),
        ),
        CardTitle(title: "Activity Level"),
        RadioGroup(
          options: getOptions(widget.options, "activity_levels"),
          value: state.userProfile.activityLevel,
          onChanged: _handActivityLevel,
        ),
      ],
    );
  }
}

///单选列表
class RadioGroup extends StatelessWidget {
  final List<String> options;
  final String value;
  final int crossAxisCount;
  final Function(String) onChanged;

  const RadioGroup({
    super.key,
    required this.options,
    required this.value,
    this.crossAxisCount = 3,
    required this.onChanged,
  });

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisSpacing: 10,
        mainAxisSpacing: 10,
        crossAxisCount: crossAxisCount,
        mainAxisExtent: 40,
      ),
      itemBuilder: (context, index) {
        var data = options[index];
        var isSelected = value == data;
        return InkWell(
          onTap: () {
            onChanged(data);
          },
          child: Container(
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: isSelected ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.surfaceContainerLow,
              borderRadius: BorderRadius.circular(8),
              border: Border.all(color: isSelected ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.surfaceContainer),
            ),
            child: Text(data, style: TextStyle(color: isSelected ? Colors.white : Colors.black)),
          ),
        );
      },
      itemCount: options.length,
    );
  }
}
