import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/features/shifts/data/models/event_tag.dart';
import 'package:krow/features/shifts/domain/shift_entity.dart';

class ShiftTagsWidget extends StatelessWidget {
  final ShiftEntity viewModel;

  final bool scrollable;

  const ShiftTagsWidget(this.viewModel, {this.scrollable = false, super.key});

  final textColors = const {
    '1': AppColors.statusWarningBody,
    '2': AppColors.statusError,
    '3': AppColors.statusSuccess,
  };
  final backGroundsColors = const {
    '1': AppColors.tintYellow,
    '2': AppColors.tintRed,
    '3': AppColors.tintGreen,
  };

  @override
  Widget build(BuildContext context) {
    if (viewModel.tags == null || viewModel.tags!.isEmpty) {
      return const SizedBox.shrink();
    }
    return Column(
      children: [
        const Gap(12),
        if (scrollable) _buildScrollableList(),
        if (!scrollable) _buildChips(),
      ],
    );
  }

  Widget _buildScrollableList() {
    return SizedBox(
      height: 44,
      child: ListView.builder(
        padding: const EdgeInsets.only(left: 16, right: 12),
        itemCount: viewModel.tags?.length ?? 0,
        scrollDirection: Axis.horizontal,
        itemBuilder: (context, index) {
          return _buildTag(viewModel.tags![index]);
        },
      ),
    );
  }

  Widget _buildChips() {
    return Padding(
      padding: const EdgeInsets.only(left: 16, right: 12),
      child: Wrap(
        runSpacing: 4,
        children: viewModel.tags!.map((e) => _buildTag(e)).toList(),
      ),
    );
  }

  Widget _buildTag(EventTag shiftTage) {
    return Container(
      margin: const EdgeInsets.only(right: 4),
      padding: const EdgeInsets.all(8),
      height: 44,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        color: backGroundsColors[shiftTage.id]??AppColors.tintGreen,
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            height: 28,
            width: 28,
            decoration: const BoxDecoration(
              shape: BoxShape.circle,
              color: AppColors.grayWhite,
            ),
            child: Center(
              child: Assets.images.icons.eye.svg(
                  width: 12,
                  height: 12,
                  colorFilter: ColorFilter.mode(
                      textColors[shiftTage.id] ?? AppColors.statusWarningBody,
                      BlendMode.srcIn)),
            ),
          ),
          const Gap(8),
          Text(
            shiftTage.name,
            style: AppTextStyles.bodySmallReg
                .copyWith(color: textColors[shiftTage.id]??AppColors.statusSuccess),
          ),
          const Gap(8),
        ],
      ),
    );
  }
}
