import 'package:flutter/material.dart';
import 'package:resume/services/breakpoints.dart';
import 'package:resume/constants.dart' show ContentType;

import '../services/tools.dart';

class ContentListTile extends StatelessWidget {
  const ContentListTile.education(
      {super.key,
      required this.name,
      required this.location,
      required this.title,
      required this.startDate,
      required this.endDate})
      : description = '',
        percentage = '',
        _contentType = ContentType.education;

  const ContentListTile.experience(
      {super.key,
      required this.name,
      required this.location,
      required this.title,
      required this.description,
      required this.startDate,
      required this.endDate})
      : percentage = '',
        _contentType = ContentType.experience;

  const ContentListTile.skills(
      {super.key, required this.name, required this.percentage})
      : description = '',
        title = ',',
        location = '',
        startDate = '',
        endDate = '',
        _contentType = ContentType.skills;

  final String description;
  final String endDate;
  final String location;
  final String name;
  final String percentage;
  final String startDate;
  final String title;

  final ContentType _contentType;

  Widget get _skillsListTile => ListTile(
        contentPadding: const EdgeInsets.all(0),
        title: Row(
          children: [
            Expanded(flex: 2, child: Text(name)),
            const Padding(padding: EdgeInsets.only(bottom: 8)),
            Expanded(
              flex: 4,
              child: Padding(
                padding: const EdgeInsets.only(left: 8.0),
                child: LinearProgressIndicator(
                  value: (double.tryParse(percentage) ?? 1) / 100,
                ),
              ),
            ),
          ],
        ),
      );

  Widget _getEducationListTile(BuildContext context, double screenWidth) =>
      ListTile(
        contentPadding: const EdgeInsets.all(0),
        title: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              name,
              style: Theme.of(context).textTheme.titleLarge,
              maxLines: 2,
            ),
            Text(
              location,
              style: Theme.of(context).textTheme.bodyMedium,
              maxLines: 2,
            ),
            Text(
              title,
              style: TextStyle(color: Theme.of(context).colorScheme.primary),
            ),
          ],
        ),
        titleAlignment: ListTileTitleAlignment.titleHeight,
        subtitle: Breakpoints.sm >= screenWidth
            ? Padding(
                padding: const EdgeInsets.only(bottom: 8),
                child: Text(
                  Tools.buildTimeString(startDate, endDate, context),
                  style: Theme.of(context).textTheme.labelSmall!.copyWith(
                        color: Theme.of(context).colorScheme.secondary,
                      ),
                ),
              )
            : null,
        trailing: Breakpoints.sm < screenWidth
            ? Text(
                Tools.buildTimeString(startDate, endDate, context),
                style:
                    TextStyle(color: Theme.of(context).colorScheme.secondary),
              )
            : null,
      );

  Widget _getExperienceListTile(BuildContext context, double screenWidth) =>
      ListTile(
        contentPadding: const EdgeInsets.all(0),
        title: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              name,
              style: Theme.of(context).textTheme.titleLarge,
            ),
            Text(
              location,
              style: Theme.of(context).textTheme.bodyMedium,
            ),
            Text(
              title,
              style: TextStyle(color: Theme.of(context).colorScheme.primary),
            ),
          ],
        ),
        titleAlignment: ListTileTitleAlignment.titleHeight,
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Padding(padding: EdgeInsets.only(top: 8)),
            if (Breakpoints.sm >= screenWidth)
              Padding(
                padding: const EdgeInsets.only(bottom: 8),
                child: Text(
                  Tools.buildTimeString(startDate, endDate, context),
                  style: Theme.of(context).textTheme.labelSmall!.copyWith(
                        color: Theme.of(context).colorScheme.secondary,
                      ),
                ),
              ),
            Text(description),
          ],
        ),
        trailing: Breakpoints.sm < screenWidth
            ? Text(
                Tools.buildTimeString(startDate, endDate, context),
                style:
                    TextStyle(color: Theme.of(context).colorScheme.secondary),
              )
            : null,
      );

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width;

    if (_contentType == ContentType.skills) {
      return _skillsListTile;
    } else if (_contentType == ContentType.education) {
      return _getEducationListTile(context, width);
    } else if (_contentType == ContentType.experience) {
      return _getExperienceListTile(context, width);
    } else {
      return const Placeholder();
    }
  }
}
