import 'dart:convert';

import 'package:drift/drift.dart' hide Column;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:sendtrain/daos/sessions_dao.dart';
import 'package:sendtrain/database/database.dart';
import 'package:sendtrain/extensions/string_extensions.dart';
import 'package:sendtrain/helpers/widget_helpers.dart';
import 'package:sendtrain/widgets/achievements/achievement_editor.dart';
import 'package:sendtrain/widgets/builders/dialogs.dart';

class SessionViewAchievements extends StatelessWidget {
  const SessionViewAchievements(
      {super.key, required this.session, this.callback});

  final Session session;
  final Function? callback;

  Session updateAchievements(int index, List achievements) {
    achievements.removeAt(index);
    return session.copyWith(
        achievements: Value<String>(json.encode(achievements)));
  }

  @override
  Widget build(BuildContext context) {
    Widget content;
    final AppDatabase db = Provider.of<AppDatabase>(context, listen: false);
    List achievements = json.decode(session.achievements ?? "[]");

    if (achievements.isEmpty) {
      content = Padding(
          padding: const EdgeInsets.only(left: 10, right: 5),
          child: ActionChip(
            visualDensity: VisualDensity.compact,
            avatar: const Icon(Icons.check_circle_outline),
            label: Text(maxLines: 1, 'Add Achievements!'),
            onPressed: () {
              showEditorSheet(context,
                  AchievementEditor(session: session, callback: callback));
            },
          ));
    } else {
      content = ListView.builder(
        scrollDirection: Axis.horizontal,
        padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
        itemCount: achievements.length,
        itemBuilder: (BuildContext context, int index) {
          return Padding(
              padding: const EdgeInsets.only(right: 5),
              child: ActionChip(
                  visualDensity: VisualDensity.compact,
                  avatar: const Icon(Icons.check_circle_outline),
                  label: Text(
                      maxLines: 1,
                      achievements[index].toString().toTitleCase()),
                  onPressed: () async {
                    // remove the achievement at index
                    // then update session
                    Session newSession =
                        updateAchievements(index, achievements);
                    await showUpdateDialog(
                        'Achievement Removal',
                        'Would you like to remove this achievement?',
                        context, () {
                      SessionsDao(db).replace(newSession);
                      if (callback != null) {
                        callback!();
                      }
                    });
                  }));
        },
      );
    }

    return Column(
      children: [
        Padding(
            padding: const EdgeInsets.only(bottom: 10),
            child: SizedBox(height: 40, child: content)),
      ],
    );
  }
}
