import 'package:flutter/material.dart';
import 'package:food_health/config/theme/custom_colors.dart';
import 'package:food_health/providers/user_store.dart';
import 'package:provider/provider.dart';
import 'package:remixicon/remixicon.dart';

import 'widget/title_card.dart';
import 'widget/user_card.dart';

class MyPage extends StatefulWidget {
  const MyPage({super.key});

  @override
  State<MyPage> createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return SafeArea(
      child: ListView(
        padding: EdgeInsets.all(15),
        children: [
          UserCard(),
          Consumer<UserStore>(
              builder: (context, store, _) {
                return Column(
                  children: [
                    TitleCard(
                      title: "Food Allergies",
                      icon: Icon(
                        RemixIcons.shield_line,
                        color: Theme
                            .of(context)
                            .colorScheme
                            .danger,
                      ),
                      child: buildTagList(
                        emptyText: "No allergies reported",
                        tags: store.profile.foodAllergiesList,
                        color: Theme
                            .of(context)
                            .colorScheme
                            .danger,
                      ),
                    ),
                    TitleCard(
                      title: "No preferences",
                      icon: Icon(
                        RemixIcons.heart_line,
                        color: Theme
                            .of(context)
                            .colorScheme
                            .success,
                      ),
                      child: buildTagList(
                        emptyText: "No dietary preferences reported",
                        tags: store.profile.dietaryPreferencesList,
                        color: Theme
                            .of(context)
                            .colorScheme
                            .success,
                      ),
                    ),
                    TitleCard(
                      title: "Medical Information",
                      icon: Icon(
                        RemixIcons.user_line,
                        color: Theme
                            .of(context)
                            .colorScheme
                            .primary,
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            margin: EdgeInsets.only(bottom: 10),
                            child: Text("Medical Conditions"),
                          ),
                          Container(
                            margin: EdgeInsets.only(bottom: 10),
                            child: buildTagList(
                              emptyText: "No medical conditions reported",
                              tags: store.profile.medicalInformationList,
                              color: Theme
                                  .of(context)
                                  .colorScheme
                                  .primary,
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(bottom: 10),
                            child: Text("Current Medications"),
                          ),
                          buildTagList(
                            emptyText: "No medications reported",
                            tags: store.profile.currentMedicationsList,
                            color: Theme
                                .of(context)
                                .colorScheme
                                .primary,
                          ),
                        ],
                      ),
                    ),
                  ],
                );
              }
          ),
        ],
      ),
    );
  }

  Widget buildTagList({
    required String emptyText,
    required List<String> tags,
    required Color color,
  }) {
    if (tags.isEmpty) {
      return Text(
        emptyText,
        style: TextStyle(
          color: Theme.of(context).colorScheme.success,
        ),
      );
    } else {
      return Wrap(
        spacing: 15,
        runSpacing: 15,
        children: tags.map((item) {
          return Container(
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
            decoration: BoxDecoration(
              color: color.withValues(alpha: 0.2),
              borderRadius: BorderRadius.circular(20),
            ),
            child: Text(
              item,
              style: TextStyle(
                color: color,
                fontSize: 14,
                fontWeight: FontWeight.w700,
              ),
            ),
          );
        }).toList(),
      );
    }
  }

  @override
  bool get wantKeepAlive => true;
}
