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/core/presentation/widgets/ui_kit/check_box.dart';

class CheckBoxCard extends StatelessWidget {
  final bool isChecked;
  final CheckBoxStyle? checkBoxStyle;
  final String title;
  final String? message;
  final String? errorMessage;
  final VoidCallback onTap;
  final bool trailing;
  final EdgeInsets padding;

  const CheckBoxCard({
    super.key,
    required this.isChecked,
    required this.title,
    required this.onTap,
    this.checkBoxStyle,
    this.message,
    this.errorMessage,
    this.trailing = false,
    this.padding = EdgeInsets.zero,
  });

  Color get titleColor {
    if (isChecked) {
      switch (checkBoxStyle ?? CheckBoxStyle.green) {
        case CheckBoxStyle.green:
          if (message != null) {
            return AppColors.blackBlack;
          }
          return AppColors.statusSuccess;
        case CheckBoxStyle.black:
          return AppColors.blackBlack;
        case CheckBoxStyle.red:
          return AppColors.blackBlack;
      }
    } else {
      switch (checkBoxStyle ?? CheckBoxStyle.green) {
        case CheckBoxStyle.green:
          return AppColors.blackBlack;
        case CheckBoxStyle.black:
          return AppColors.blackGray;
        case CheckBoxStyle.red:
          return AppColors.blackBlack;
      }
    }
  }

  Color get messageColor {
    if (errorMessage != null) {
      return AppColors.statusError;
    } else if (isChecked) {
      return AppColors.statusSuccess;
    } else {
      return AppColors.blackGray;
    }
  }

  Color get backgroundMessageColor {
    if (message == null &&
        errorMessage == null &&
        isChecked &&
        checkBoxStyle == CheckBoxStyle.green) {
      return AppColors.tintGreen;
    } else {
      return AppColors.grayPrimaryFrame;
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        constraints: const BoxConstraints(minHeight: 50),
        margin: padding,
        padding: const EdgeInsets.all(12),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8),
          color: backgroundMessageColor,
        ),
        child: Row(
          crossAxisAlignment: message != null || errorMessage != null
              ? CrossAxisAlignment.start
              : CrossAxisAlignment.center,
          children: [
            KWCheckBox(
                value: isChecked, style: checkBoxStyle ?? CheckBoxStyle.green),
            const Gap(8),
            Expanded(
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Text(title,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: AppTextStyles.bodyMediumMed.copyWith(
                          color: titleColor,
                        )),
                    if (message != null || errorMessage != null) const Gap(2),
                    if (message != null || errorMessage != null)
                      Text(
                        errorMessage ?? message!,
                        style: AppTextStyles.bodyTinyReg
                            .copyWith(color: messageColor),
                      ),
                  ]),
            ),
            if (trailing) Padding(
              padding: const EdgeInsets.only(left:24.0),
              child: Assets.images.arrowRight.svg(),
            )
          ],
        ),
      ),
    );
  }
}
