import 'package:fcs/domain/entities/custom.dart';
import 'package:fcs/domain/entities/discount_rate.dart';
import 'package:fcs/domain/entities/rate.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/rates/model/shipment_rate_model.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../main/util.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);

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(
              Icons.close,
            ),
            onPressed: () => Navigator.of(context).pop(),
          ),
          backgroundColor: primaryColor,
          title: LocalText(
            context,
            "rate.title",
            color: Colors.white,
            fontSize: 18,
          ),
        ),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: ListView(
            // crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Container(
                padding: EdgeInsets.only(left: 25, top: 10),
                child: Text(
                  "Cargo Types",
                  style: TextStyle(
                      color: primaryColor,
                      fontWeight: FontWeight.bold,
                      fontSize: 15),
                ),
              ),
              Container(
                height: 135,
                child: Column(
                  children: getCargoWidget(shipmentRateModel.rates),
                ),
              ),
              Divider(
                color: Colors.grey,
              ),
              Container(
                padding: EdgeInsets.only(left: 25, top: 10),
                child: Text(
                  "Custom Duties",
                  style: TextStyle(
                      color: primaryColor,
                      fontWeight: FontWeight.bold,
                      fontSize: 15),
                ),
              ),
              Container(
                height: 100,
                child: Column(
                  children: getCustonWidget(shipmentRateModel.customs),
                ),
              ),
              Divider(
                color: Colors.grey,
              ),
              Container(
                padding: EdgeInsets.only(left: 25, top: 10),
                child: Text(
                  "Discounts by weight",
                  style: TextStyle(
                      color: primaryColor,
                      fontWeight: FontWeight.bold,
                      fontSize: 15),
                ),
              ),
              Container(
                height: 100,
                child: Column(
                  children:
                      getDiscountWidget(shipmentRateModel.discountsByWeight),
                ),
              ),
              Divider(
                color: Colors.grey,
              ),
              _row("Free delivery within Yangon \nfor shipments over", "10",
                  "pounds"),
              _row("Delivery fees", "\$ 5", "below 10 pounds"),
              _row("Volumetric Ratio", "166.36", "in3 per pound"),
              // fcsButton(context, "Terms & Conditions", callack: () {
              //   Navigator.of(context)
              //       .push(MaterialPageRoute(builder: (_) => Term()));
              // }),
              fcsButton(context, "Estimate shipping cost", callack: () {
                Navigator.of(context)
                    .push(BottomUpPageRoute(ShipmentRatesCal()));
              }),
              fcsButton(context, "Edit", callack: () {
                Navigator.of(context)
                    .push(BottomUpPageRoute(ShipmentRatesEdit()));
              }),
              SizedBox(height: 10)
            ],
          ),
        ),
      ),
    );
  }

  List<Widget> getCargoWidget(List<Rate> rates) {
    return rates.map((cargo) {
      return Container(
        child: _row(
            cargo.description, "\$ " + cargo.price.toString(), 'per pound'),
      );
    }).toList();
  }

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

  List<Widget> getDiscountWidget(List<DiscountRate> discounts) {
    return discounts.map((d) {
      return Container(
        child: _row(
            "${d.weight.toString()} lb", "\$ " + d.discountRate.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,
            ),
          ],
        ));
  }
}
