import 'package:flutter/material.dart';
import 'package:isar/isar.dart';

import '../../../consts/consts.dart';
import '../../../entity/area/area.dart';
import '../../../entity/area/area_repository.dart';
import '../../../packages/ambito_api/base_api.dart';
import '../../../packages/ambito_theme/ambito_theme.dart';
import '../../buttons/outline_button.dart';

class EditAreaNoticeDialog extends StatelessWidget {
  final TextEditingController _controllerDescription = TextEditingController();
  final int id;

  EditAreaNoticeDialog({super.key, required this.id});

  @override
  Widget build(BuildContext context) {
    final AmbitoTheme theme = getTheme(context);

    Area? area = isar.areas.where().idEqualTo(id).findFirst();
    if (area != null) {
      _controllerDescription.text = area.description!;
    }

    return Dialog(
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.all(
          Radius.circular(10.0),
        ),
      ),
      child: SizedBox(
        width: 800,
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(
                'Notiz:',
                style: theme.headlineSmall.copyWith(
                  color: theme.currentColorScheme.primary,
                ),
              ),
              theme.verticalSpacer,
              TextField(
                controller: _controllerDescription,
                maxLines: 5,
                decoration: InputDecoration(
                  labelText: 'Notizen',
                  isDense: true,
                  hintText: 'Notizen',
                  border: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: redColors['primary']!,
                      width: 1,
                      style: BorderStyle.solid,
                    ),
                    borderRadius: BorderRadius.circular(8),
                  ),
                ),
              ),
              theme.verticalSpacer,
              Align(
                alignment: Alignment.centerRight,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    WidgetOutlineButton(
                      onPressed: () {
                        Navigator.pop(context);
                      },
                      title: 'Abbrechen',
                      backgroundColor: theme.currentColorScheme.surface
                        ..withValues(alpha: .1),
                      foregroundColor: theme.currentColorScheme.primary,
                      borderColor: theme.currentColorScheme.primary,
                    ),
                    theme.horizontalSpacer,
                    WidgetOutlineButton(
                      onPressed: () {
                        Area area = isar.areas.get(id)!;
                        area.description = _controllerDescription.value.text;
                        AreaRepository().put(area);
                        BaseApi()
                            .patchContent('area', id, area.toJson())
                            .then((_) {
                          Navigator.pop(context);
                        });
                      },
                      title: 'Speichern',
                      backgroundColor: theme.currentColorScheme.secondary,
                      foregroundColor: theme.currentColorScheme.onPrimary,
                      borderColor: theme.currentColorScheme.secondary,
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
