import 'package:refilc/helpers/subject.dart';
import 'package:refilc/models/settings.dart';
import 'package:refilc/theme/colors/colors.dart';
import 'package:refilc_kreta_api/models/grade.dart';
import 'package:refilc/ui/widgets/grade/grade_tile.dart';
import 'package:refilc_mobile_ui/pages/grades/subject_grades_container.dart';
import 'package:flutter/material.dart';
import 'package:flutter_feather_icons/flutter_feather_icons.dart';
import 'package:refilc/utils/format.dart';
import 'package:provider/provider.dart';
import 'certification_tile.i18n.dart';

class CertificationTile extends StatelessWidget {
  const CertificationTile(
    this.grade, {
    super.key,
    this.onTap,
    this.padding,
    this.newStyle = false,
  });

  final Function()? onTap;
  final Grade grade;
  final EdgeInsetsGeometry? padding;
  final bool newStyle;

  @override
  Widget build(BuildContext context) {
    bool isSubjectView = SubjectGradesContainer.of(context) != null;
    String certificationName;

    SettingsProvider settingsProvider = Provider.of<SettingsProvider>(context);

    switch (grade.type) {
      case GradeType.endYear:
        certificationName = "final".i18n;
        break;
      case GradeType.halfYear:
        certificationName = "mid".i18n;
        break;
      case GradeType.firstQ:
        certificationName = "1q".i18n;
        break;
      case GradeType.secondQ:
        certificationName = "2q".i18n;
        break;
      case GradeType.thirdQ:
        certificationName = "3q".i18n;
        break;
      case GradeType.fourthQ:
        certificationName = "4q".i18n;
        break;
      case GradeType.levelExam:
        certificationName = "equivalency".i18n;
        break;
      case GradeType.unknown:
      default:
        certificationName = "unknown".i18n;
    }

    return Material(
      color: Theme.of(context).colorScheme.surface,
      borderRadius: BorderRadius.circular(8.0),
      child: Padding(
        padding: padding ?? const EdgeInsets.symmetric(horizontal: 8.0),
        child: ListTile(
          tileColor: gradeColor(
                  context: context, value: grade.value.value, nocolor: false)
              .withValues(alpha: .1),
          visualDensity: VisualDensity.compact,
          contentPadding: isSubjectView
              ? const EdgeInsets.only(left: 20.0, right: 12.0, bottom: 6.0)
              : const EdgeInsets.only(left: 8.0, right: 12.0),
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
          onTap: onTap,
          leading: isSubjectView
              ? GradeValueWidget(
                  grade.value,
                  complemented: grade.description == 'Dicséret',
                )
              : Padding(
                  padding: const EdgeInsets.only(left: 2.0),
                  child: Icon(
                      SubjectIcon.resolveVariant(
                          subject: grade.subject, context: context),
                      size: 28.0,
                      color: AppColors.of(context).text.withValues(alpha: .75)),
                ),
          minLeadingWidth: isSubjectView ? 32.0 : 42.0,
          trailing: isSubjectView
              ? const Icon(FeatherIcons.award)
              : GradeValueWidget(
                  grade.value,
                  complemented: grade.description == 'Dicséret',
                ),
          title: Text(
              isSubjectView
                  ? certificationName
                  : grade.subject.renamedTo ?? grade.subject.name.capital(),
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                  fontWeight: FontWeight.w700,
                  fontSize: 18.0,
                  fontStyle: grade.subject.isRenamed &&
                          settingsProvider.renamedSubjectsItalics
                      ? FontStyle.italic
                      : null)),
          subtitle: Text(grade.value.valueName,
              style:
                  const TextStyle(fontWeight: FontWeight.w600, fontSize: 16.0)),
        ),
      ),
    );
  }
}
