import 'package:fcs/domain/entities/carton.dart';
import 'package:fcs/domain/entities/rate.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/local_title.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

typedef OnSelect = Function(Carton carton, bool checked);

class InvoiceCartonTable extends StatelessWidget {
  final List<Carton> cartons;
  final OnSelect onSelect;
  final Rate rate;

  const InvoiceCartonTable({Key key, this.cartons, this.onSelect, this.rate})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    final tableTitle = Container(
      padding: EdgeInsets.only(right: 10.0, top: 5),
      child: Row(
        children: <Widget>[
          SizedBox(
            width: 50,
          ),
          SizedBox(
            width: 150,
            child: LocalText(context, 'invoice.box.number', color: Colors.grey),
          ),
          LocalText(context, 'invoice.shipment_weight', color: Colors.grey),
        ],
      ),
    );

    final rows = cartons == null
        ? [Container()]
        : cartons.asMap().entries.map((p) {
            return Container(
              color: p.value.isChecked
                  ? Colors.grey.withOpacity(0.2)
                  : Colors.grey[50].withOpacity(0.2),
              child: Container(
                padding: EdgeInsets.only(
                    left: 0.0, right: 10.0, top: 3.0, bottom: 3.0),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                        color: p.key == cartons.length - 1
                            ? Colors.white
                            : Colors.grey[350],
                        width: 1),
                  ),
                ),
                child: Row(
                  children: <Widget>[
                    onSelect == null
                        ? p.value.isChecked
                            ? SizedBox(
                                child: Icon(Icons.check, color: primaryColor),
                                width: 30)
                            : SizedBox(width: 30)
                        : Checkbox(
                            value: p.value.isChecked,
                            activeColor: primaryColor,
                            onChanged: (bool check) {
                              if (onSelect != null) onSelect(p.value, check);
                            }),
                    Expanded(
                        child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          p.value.cartonNumber,
                          style: textStyle,
                        ),
                      ],
                    )),
                    Flexible(
                      child: new Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          new Text(
                            "${p.value?.length ?? ""} x ${p.value?.width ?? ""} x ${p.value?.height ?? ""}",
                            style: textStyle,
                          ),
                          new Text(
                            "${p.value?.getShipmentWeight(rate.volumetricRatio)?.toStringAsFixed(2) ?? "0"} lb",
                            style: textStyle,
                          ),
                          new Text(
                            "${p.value?.actualWeight?.toStringAsFixed(2) ?? "0"} lb (Actual)",
                            style: textStyle,
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              ),
            );
          }).toList();

    return Column(
      children: [
        LocalTitle(textKey: "invoice.box_info"),
        tableTitle,
        Divider(
          color: Colors.grey[400],
        ),
        Column(
          children: rows,
        ),
      ],
    );
  }
}
