import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';

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

class MeasureMonthsDatasource extends DataGridSource {
  MeasureMonthsDatasource(
      {required List<MeasureMonths> areas,
      required this.context,
      required this.type}) {
    dataGridRows = areas
        .map<DataGridRow>(
          (dataGridRow) => DataGridRow(
            cells: [
              DataGridCell<String>(
                columnName: 'measure',
                value: dataGridRow.name,
              ),
              DataGridCell<bool>(
                columnName: 'Jan',
                value: type == 'apply'
                    ? dataGridRow.applyList![0]
                    : dataGridRow.maintenanceList![0],
              ),
              DataGridCell<bool>(
                columnName: 'Feb',
                value: type == 'apply'
                    ? dataGridRow.applyList![1]
                    : dataGridRow.maintenanceList![1],
              ),
              DataGridCell<bool>(
                columnName: 'Mär',
                value: type == 'apply'
                    ? dataGridRow.applyList![2]
                    : dataGridRow.maintenanceList![2],
              ),
              DataGridCell<bool>(
                columnName: 'Apr',
                value: type == 'apply'
                    ? dataGridRow.applyList![3]
                    : dataGridRow.maintenanceList![3],
              ),
              DataGridCell<bool>(
                columnName: 'Mai',
                value: type == 'apply'
                    ? dataGridRow.applyList![4]
                    : dataGridRow.maintenanceList![4],
              ),
              DataGridCell<bool>(
                columnName: 'Jun',
                value: type == 'apply'
                    ? dataGridRow.applyList![5]
                    : dataGridRow.maintenanceList![5],
              ),
              DataGridCell<bool>(
                columnName: 'Jul',
                value: type == 'apply'
                    ? dataGridRow.applyList![6]
                    : dataGridRow.maintenanceList![6],
              ),
              DataGridCell<bool>(
                columnName: 'Aug',
                value: type == 'apply'
                    ? dataGridRow.applyList![7]
                    : dataGridRow.maintenanceList![7],
              ),
              DataGridCell<bool>(
                columnName: 'Sep',
                value: type == 'apply'
                    ? dataGridRow.applyList![8]
                    : dataGridRow.maintenanceList![8],
              ),
              DataGridCell<bool>(
                columnName: 'Okt',
                value: type == 'apply'
                    ? dataGridRow.applyList![9]
                    : dataGridRow.maintenanceList![9],
              ),
              DataGridCell<bool>(
                columnName: 'Nov',
                value: type == 'apply'
                    ? dataGridRow.applyList![10]
                    : dataGridRow.maintenanceList![10],
              ),
              DataGridCell<bool>(
                columnName: 'Dez',
                value: type == 'apply'
                    ? dataGridRow.applyList![11]
                    : dataGridRow.maintenanceList![11],
              ),
            ],
          ),
        )
        .toList();
  }

  final BuildContext context;
  final String type;

  List<DataGridRow> dataGridRows = [];

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

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    final AmbitoTheme theme = getTheme(context);

    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>(
      (dataGridCell) {
        if (dataGridCell.columnName == 'measure') {
          return Container(
            alignment: Alignment.centerLeft,
            padding: const EdgeInsets.symmetric(horizontal: 10.0),
            child: InkWell(
              child: Text(
                dataGridCell.value,
                overflow: TextOverflow.ellipsis,
                style: theme.bodyMedium.copyWith(
                  color: theme.currentColorScheme.primary,
                ),
              ),
            ),
          );
        }
        Widget? content;
        if (type == 'apply' && dataGridCell.value == true) {
          content = Icon(
            Icons.carpenter_outlined,
            color: theme.currentColorScheme.secondary,
          );
        }
        if (type == 'maintenance' && dataGridCell.value == true) {
          content = Icon(
            Icons.eco_outlined,
            color: theme.currentColorScheme.secondary,
          );
        }
        return Container(
          alignment: Alignment.centerLeft,
          padding: const EdgeInsets.symmetric(horizontal: 10.0),
          child: content ?? SizedBox(),
        );
      },
    ).toList());
  }
}
