import 'package:ambito/src/entity/area/area.dart';
import 'package:ambito/src/widgets/dialogs/delete_entity/delete_entity_dialog.dart';
import 'package:ambito/src/widgets/dialogs/edit_entity_dialog/edit_entity_dialog.dart';
import 'package:ambito/src/widgets/dialogs/edit_field_dialog/edit_field_dialog.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:isar/isar.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';

import '../../packages/ambito_theme/ambito_theme.dart';

class AreaDataSource extends DataGridSource {
  AreaDataSource({required List<Area> areas, required this.context}) {
    dataGridRows = areas
        .map<DataGridRow>(
          (dataGridRow) => DataGridRow(
            cells: [
              DataGridCell<int>(
                columnName: 'id',
                value: dataGridRow.id,
              ),
              DataGridCell<String>(
                columnName: 'name',
                value: dataGridRow.name,
              ),
              DataGridCell<String>(
                columnName: 'size',
                value: dataGridRow.size,
              ),
              DataGridCell<String>(
                columnName: 'description',
                value: dataGridRow.description,
              ),
              const DataGridCell(
                columnName: 'action',
                value: '',
              ),
            ],
          ),
        )
        .toList();
  }

  final BuildContext context;

  List<DataGridRow> dataGridRows = [];

  @override
  List<DataGridRow> get rows => dataGridRows;

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    final AmbitoTheme theme = getTheme(context);
    final id = row.getCells().firstOrNull?.value;

    NumberFormat numberFormat = NumberFormat.decimalPattern('de');

    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>(
      (dataGridCell) {
        if (dataGridCell.columnName == 'action') {
          return Container(
            alignment: Alignment.centerLeft,
            padding: const EdgeInsets.symmetric(horizontal: 10.0),
            child: Row(
              children: [
                IconButton(
                  onPressed: () => EditEntityDialog.show(
                    context: context,
                    type: 'area',
                    id: id,
                  ),
                  icon: Icon(
                    Icons.edit_outlined,
                    color: theme.currentColorScheme.primary,
                  ),
                ),
                IconButton(
                  onPressed: () => DeleteEntityDialog.show(
                    context: context,
                    type: 'area',
                    id: id,
                  ),
                  icon: Icon(
                    Icons.delete_outline,
                    color: theme.currentColorScheme.primary,
                  ),
                ),
              ],
            ),
          );
        }
        if (dataGridCell.columnName == 'description') {
          return Container(
            alignment: Alignment.centerLeft,
            padding: const EdgeInsets.symmetric(horizontal: 10.0),
            child: IconButton(
              onPressed: () => EditFieldDialog.show(
                context: context,
                type: 'area_notice',
                id: id,
              ),
              icon: Icon(
                Icons.description_outlined,
                color: theme.currentColorScheme.primary,
              ),
            ),
          );
        }
        if (dataGridCell.columnName == 'size') {
          return Container(
            alignment: Alignment.centerLeft,
            padding: const EdgeInsets.symmetric(horizontal: 10.0),
            child: Text(
              '${numberFormat.format(float.parse(dataGridCell.value.toString()))} m²',
              overflow: TextOverflow.ellipsis,
              style: theme.bodyMedium.copyWith(
                color: theme.currentColorScheme.primary,
              ),
            ),
          );
        }
        return Container(
          alignment: Alignment.centerLeft,
          padding: const EdgeInsets.symmetric(horizontal: 10.0),
          child: Text(
            dataGridCell.value.toString(),
            overflow: TextOverflow.ellipsis,
            style: theme.bodyMedium.copyWith(
              color: theme.currentColorScheme.primary,
            ),
          ),
        );
      },
    ).toList());
  }
}
