import 'package:fcs/domain/entities/cargo_type.dart';
import 'package:fcs/domain/entities/custom_duty.dart';
import 'package:fcs/domain/entities/discount_by_weight.dart';
import 'package:fcs/domain/entities/rate.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/rates/cargo_type_list.dart';
import 'package:fcs/pages/rates/custom_list.dart';
import 'package:fcs/pages/rates/model/shipment_rate_model.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../main/util.dart';
import 'discount_by weight_list.dart';
import 'shipment_rates_calculate.dart';
import 'shipment_rates_edit.dart';

class ShipmentRates extends StatefulWidget {
  ShipmentRates();

  @override
  _ShipmentRatesState createState() => _ShipmentRatesState();
}

class _ShipmentRatesState extends State<ShipmentRates> {
  bool _isLoading = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var shipmentRateModel = Provider.of<ShipmentRateModel>(context);
    Rate rate = shipmentRateModel.rate;
    bool isEditable = context.select((MainModel m) => m.rateEditable());

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(CupertinoIcons.back, color: primaryColor),
            onPressed: () => Navigator.of(context).pop(),
          ),
          backgroundColor: Colors.white,
          shadowColor: Colors.transparent,
          title: LocalText(context, 'rate.title',
              color: primaryColor, fontSize: 20),
          actions: isEditable
              ? [
                  IconButton(
                      onPressed: () => Navigator.of(context).push(
                          CupertinoPageRoute(
                              builder: (context) => ShipmentRatesEdit())),
                      icon: Icon(
                        CupertinoIcons.pen,
                        color: primaryColor,
                      ))
                ]
              : [],
        ),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: ListView(
            // crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              fcsButton(context, getLocalString(context, "rate.cal.title"),
                  callack: () {
                Navigator.of(context).push(CupertinoPageRoute(
                    builder: (context) => ShipmentRatesCal()));
              }),
              Divider(
                color: Colors.grey,
              ),
              Container(
                padding: EdgeInsets.only(left: 25, top: 10, right: 25),
                child: Row(
                  children: [
                    LocalText(context, "rate.cargo.type",
                        color: primaryColor,
                        fontWeight: FontWeight.bold,
                        fontSize: 15),
                    Spacer(),
                    isEditable
                        ? IconButton(
                            icon: Icon(Icons.edit, color: primaryColor),
                            onPressed: () {
                              Navigator.of(context).push(CupertinoPageRoute(
                                  builder: (context) => CargoTypeList()));
                            })
                        : Container()
                  ],
                ),
              ),
              Column(
                children: getCargoWidget(shipmentRateModel.rate.cargoTypes),
              ),
              Divider(
                color: Colors.grey,
              ),
              Container(
                padding: EdgeInsets.only(left: 25, top: 10, right: 25),
                child: Row(
                  children: [
                    LocalText(context, "rate.discount_by_weight",
                        color: primaryColor,
                        fontWeight: FontWeight.bold,
                        fontSize: 15),
                    Spacer(),
                    isEditable
                        ? IconButton(
                            icon: Icon(Icons.edit, color: primaryColor),
                            onPressed: () {
                              Navigator.of(context).push(CupertinoPageRoute(
                                  builder: (context) =>
                                      DiscountByWeightList()));
                            })
                        : Container()
                  ],
                ),
              ),
              Column(
                children:
                    getDiscountWidget(shipmentRateModel.rate.discountByWeights),
              ),
              Divider(
                color: Colors.grey,
              ),
              _row("Free delivery within Yangon \nfor shipments over",
                  "${rate.freeDeliveryWeight}", "pounds"),
              _row("Delivery fees", "\$ ${rate.deliveryFee}",
                  "below ${rate.freeDeliveryWeight} pounds"),
              _row("Volumetric Ratio", "${rate.volumetricRatio}",
                  "in3 per pound"),
              Divider(
                color: Colors.grey,
              ),
              Container(
                padding: EdgeInsets.only(left: 25, top: 10, right: 25),
                child: Row(
                  children: [
                    LocalText(context, "rate.custom_duty",
                        color: primaryColor,
                        fontWeight: FontWeight.bold,
                        fontSize: 15),
                    Spacer(),
                    isEditable
                        ? IconButton(
                            icon: Icon(Icons.edit, color: primaryColor),
                            onPressed: () {
                              Navigator.of(context).push(CupertinoPageRoute(
                                  builder: (context) => CustomList()));
                            })
                        : Container()
                  ],
                ),
              ),
              Column(
                children: getCustonWidget(shipmentRateModel.rate.customDuties),
              ),
            ],
          ),
        ),
      ),
    );
  }

  List<Widget> getCargoWidget(List<CargoType> cargos) {
    return cargos.map((cargo) {
      return Container(
        child: _row(cargo.name, "\$ " + cargo.rate.toString(), 'per pound'),
      );
    }).toList();
  }

  List<Widget> getCustonWidget(List<CustomDuty> customs) {
    return customs.map((c) {
      return Container(
        child: _row(c.productType, "\$ " + c.fee.toString(), ''),
      );
    }).toList();
  }

  List<Widget> getDiscountWidget(List<DiscountByWeight> discounts) {
    if (discounts == null) return [];
    return discounts.map((d) {
      return Container(
        child: _row(
            "${d.weight.toString()} lb", "\$ " + d.discount.toString(), ''),
      );
    }).toList();
  }

  _row(String desc, String price, String unit) {
    return Container(
        padding: EdgeInsets.only(left: 25, top: 5, bottom: 5),
        child: Row(
          children: <Widget>[
            Text('$desc ', style: TextStyle(fontSize: 15)),
            Spacer(),
            Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(bottom: 3.0),
                  child: Text(
                    '$price',
                    style: TextStyle(color: primaryColor, fontSize: 14),
                  ),
                ),
                Text(
                  '$unit',
                  style: TextStyle(color: Colors.grey, fontSize: 14),
                ),
              ],
            ),
            SizedBox(
              width: 50,
            ),
          ],
        ));
  }
}
