import 'package:ambito/src/widgets/buttons/save_buttons_group.dart';
import 'package:ambito/src/widgets/texts/text_headline.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:isar/isar.dart';
import 'package:screen_breakpoints/screen_breakpoints.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 '../../appbar/ambito_appbar.dart';

class EditAreaDialog extends StatelessWidget {
  final TextEditingController _controllerName = TextEditingController();
  final TextEditingController _controllerSize = TextEditingController();
  final TextEditingController _controllerDescription = TextEditingController();
  final int id;

  EditAreaDialog({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) {
      _controllerName.text = area.name!;
      _controllerDescription.text = area.description!;
      _controllerSize.text = area.size!;
    }

    return Dialog.fullscreen(
      child: Scaffold(
        appBar: AmbitoAppbar(
            links: const ['dashboard', 'massnahmen'],
            breakpoint: Breakpoint.fromContext(context),
            theme: theme),
        body: BreakpointBuilder(
          builder: (
            context,
            breakpoint,
            configuration,
          ) {
            return SingleChildScrollView(
              child: Center(
                child: SizedBox(
                  width: Breakpoint.fromContext(context).width,
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        theme.verticalSpacer,
                        const TextHeadline(
                            text:
                                'page.dashboard.business.area.title_edit_area'),
                        theme.verticalSpacer,
                        SaveButtonsGroup(onPressed: () {
                          Area area = isar.areas.get(id)!;
                          area.description = _controllerDescription.value.text;
                          area.name = _controllerName.value.text;
                          area.size = _controllerSize.value.text;
                          AreaRepository().put(area);
                          BaseApi()
                              .patchContent('area', id, area.toJson())
                              .then((_) {
                            Navigator.pop(context);
                            Get.offAndToNamed('/dashboard/flaechen/1');
                          });
                        }),
                        theme.verticalSpacer,
                        Card(
                          elevation: 0,
                          color: theme.currentColorScheme.tertiary,
                          child: Padding(
                            padding: const EdgeInsets.all(20),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  'Allgemein',
                                  style: theme.headlineSmall,
                                ),
                                theme.verticalSpacer,
                                Row(
                                  children: [
                                    Expanded(
                                      child: TextField(
                                        controller: _controllerName,
                                        maxLines: 1,
                                        decoration: InputDecoration(
                                          labelText: 'Bezeichnung',
                                          isDense: true,
                                          hintText: 'Bezeichnung',
                                          filled: true,
                                          fillColor: Colors.white,
                                          hoverColor: Colors.white,
                                          border: OutlineInputBorder(
                                            borderSide: BorderSide(
                                              color: redColors['primary']!,
                                              width: 1,
                                              style: BorderStyle.solid,
                                            ),
                                            borderRadius:
                                                BorderRadius.circular(8),
                                          ),
                                        ),
                                      ),
                                    ),
                                    theme.horizontalSpacer,
                                    Expanded(
                                      child: TextField(
                                        controller: _controllerSize,
                                        maxLines: 1,
                                        decoration: InputDecoration(
                                          suffixText: 'm²',
                                          labelText: 'Größe',
                                          isDense: true,
                                          hintText: 'Größe',
                                          filled: true,
                                          fillColor: Colors.white,
                                          hoverColor: Colors.white,
                                          border: OutlineInputBorder(
                                            borderSide: BorderSide(
                                              color: redColors['primary']!,
                                              width: 1,
                                              style: BorderStyle.solid,
                                            ),
                                            borderRadius:
                                                BorderRadius.circular(8),
                                          ),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                                theme.verticalSpacer,
                                TextField(
                                  controller: _controllerDescription,
                                  maxLines: 5,
                                  decoration: InputDecoration(
                                    labelText: 'Notizen',
                                    isDense: true,
                                    hintText: 'Notizen',
                                    filled: true,
                                    fillColor: Colors.white,
                                    hoverColor: Colors.white,
                                    border: OutlineInputBorder(
                                      borderSide: BorderSide(
                                        color: redColors['primary']!,
                                        width: 1,
                                        style: BorderStyle.solid,
                                      ),
                                      borderRadius: BorderRadius.circular(8),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                        theme.verticalSpacer,
                      ],
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}
