import 'package:fcs/domain/entities/cargo_type.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/my_data_table.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'cargo_type_editor.dart';

typedef OnAdd(CargoType cargoType);
typedef OnRemove(CargoType cargoType);

class CargoTable extends StatelessWidget {
  final List<CargoType> cargoTypes;
  final OnAdd onAdd;
  final OnRemove onRemove;

  const CargoTable({Key key, this.cargoTypes, this.onAdd, this.onRemove})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MyDataTable(
      headingRowHeight: 40,
      columns: [
        MyDataColumn(
          label: LocalText(
            context,
            "cargo.type",
            color: Colors.grey,
          ),
        ),
        MyDataColumn(
          label: LocalText(
            context,
            "cargo.weight",
            color: Colors.grey,
          ),
        ),
      ],
      rows: getCargoRows(context),
    );
  }

  List<MyDataRow> getCargoRows(BuildContext context) {
    if (cargoTypes == null) {
      return [];
    }
    double total = 0;
    var rows = cargoTypes.map((c) {
      total += c.weight;
      return MyDataRow(
        onSelectChanged: (bool selected) async {
          CargoType cargo = await Navigator.push<CargoType>(
              context,
              CupertinoPageRoute(
                  builder: (context) => CargoTypeEditor(
                        cargo: c,
                      )));
          if (onAdd != null) onAdd(cargo);
        },
        cells: [
          MyDataCell(new Text(
            c.name == null ? "" : c.name,
            style: textStyle,
          )),
          MyDataCell(
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Text(c.weight == null ? "0" : c.weight.toString(),
                    style: textStyle),
                onRemove == null
                    ? SizedBox(
                        width: 50,
                      )
                    : IconButton(
                        icon: Icon(
                          Icons.remove_circle,
                          color: primaryColor,
                        ),
                        onPressed: () {
                          if (onRemove != null) onRemove(c);
                        })
              ],
            ),
          ),
        ],
      );
    }).toList();

    var totalRow = MyDataRow(
      onSelectChanged: (bool selected) {},
      cells: [
        MyDataCell(Align(
          alignment: Alignment.centerRight,
          child: LocalText(
            context,
            "shipment.cargo.total",
            color: Colors.black87,
            fontWeight: FontWeight.bold,
          ),
        )),
        MyDataCell(
          Padding(
            padding: const EdgeInsets.only(right: 48.0),
            child: Align(
                alignment: Alignment.centerRight,
                child: Text(total.toString(),
                    style: TextStyle(fontWeight: FontWeight.bold))),
          ),
        ),
      ],
    );
    rows.add(totalRow);
    return rows;
  }
}
