import 'package:fcs/domain/entities/package.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(Package package, bool checked);

class CartonPackageTable extends StatelessWidget {
  final List<Package> packages;
  final OnSelect onSelect;

  const CartonPackageTable({Key key, this.packages, this.onSelect})
      : super(key: key);

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

    final rows = packages == null
        ? [Container()]
        : packages.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 == packages.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.trackingID,
                          style: textStyle,
                        ),
                        Text(
                          p.value.deliveryAddress?.fullName ?? "",
                          style: textStyle,
                        ),
                        Text(
                          p.value.deliveryAddress?.phoneNumber ?? "",
                          style: textStyle,
                        ),
                      ],
                    )),
                    Flexible(
                      child: new Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          new Text(
                            p.value?.desc ?? "",
                            style: textStyle,
                          ),
                          new Text(
                            "(${p.value?.market ?? ""})",
                            style: textStyle,
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),
            );
          }).toList();

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