import 'package:fcs/domain/entities/pickup.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/pickup/model/pickup_model.dart';
import 'package:fcs/pages/pickup/pickup_editor.dart';
import 'package:fcs/pages/widgets/defalut_delivery_address.dart';
import 'package:fcs/pages/widgets/display_text.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/multi_img_controller.dart';
import 'package:fcs/pages/widgets/multi_img_file.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

final DateFormat dateFormat = DateFormat("d MMM yyyy");

class PickupInfo extends StatefulWidget {
  final Pickup pickup;
  const PickupInfo({Key? key, required this.pickup}) : super(key: key);

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

class _PickupInfoState extends State<PickupInfo> {
  var dateFormatter = new DateFormat('dd MMM yyyy');
  var timeFormatter = new DateFormat('h:mm a');

  Pickup? _pickup;
  bool _isLoading = false;
  MultiImgController multiImgController = MultiImgController();

  @override
  void initState() {
    super.initState();
    initPackage(widget.pickup);
  }

  initPackage(Pickup pickup) {
    setState(() {
      _pickup = pickup;
      multiImgController.setImageUrls = pickup.photoUrls;
    });
  }

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

  @override
  Widget build(BuildContext context) {
    final pickupNumberBox = DisplayText(
      text: _pickup!.pickupNumber ?? "",
      labelTextKey: "pickup.pickup_number",
      iconData: SimpleLineIcons.direction,
    );

    final pickupDateBox = DisplayText(
      text: _pickup!.pickupDate == null
          ? ""
          : dateFormatter.format(_pickup!.pickupDate!),
      labelTextKey: "pickup.date",
      iconData: Icons.date_range,
    );

    var timeBox = Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Container(
          width: 150,
          child: DisplayText(
            text: _pickup!.fromTime == null
                ? ""
                : timeFormatter.format(_pickup!.fromTime!),
            labelTextKey: "pickup.from_time",
            iconData: MaterialCommunityIcons.clock_start,
          ),
        ),
        Container(
          width: 150,
          child: DisplayText(
            text: _pickup!.toTime == null
                ? ""
                : timeFormatter.format(_pickup!.toTime!),
            labelTextKey: "pickup.to_time",
            iconData: MaterialCommunityIcons.clock_end,
          ),
        )
      ],
    );

    final customerBox = DisplayText(
      text: _pickup!.customerName ?? "",
      labelTextKey: "pickup.customer",
      iconData: Icons.perm_identity,
    );

    final staffNameBox = DisplayText(
      text: _pickup!.staffName ?? "",
      labelTextKey: "pickup.staff.name",
      iconData: Icons.perm_identity,
    );

    final deliveryAddressBox = DefaultDeliveryAddress(
        deliveryAddress: _pickup!.pickupAddress,
        labelKey: "pickup.delivery.address",
        onTap: null);

    final statusBox = DisplayText(
      text: _pickup != null ? _pickup!.status : "",
      labelTextKey: "pickup.status",
      iconData: Icons.av_timer,
    );

    final img = MultiImageFile(
      enabled: false,
      controller: multiImgController,
      title: "Receipt File",
    );

    final completeRemarkBox = DisplayText(
      text: _pickup!.completeRemark ?? "",
      labelTextKey: "pickup.complete.remark",
      iconData: Entypo.new_message,
    );

    final rescheduleRemarkBox = DisplayText(
      text: _pickup!.rescheduleRemark ?? "",
      labelTextKey: "pickup.reschedul.remark",
      iconData: Entypo.new_message,
    );

    final customerRemarkBox = DisplayText(
      text: _pickup!.customerRemark ?? "",
      labelTextKey: "pickup.customer.remark",
      iconData: Entypo.new_message,
    );

    final continueBtn = fcsButton(
      context,
      getLocalString(context, 'pickup.continue.btn'),
      callack: _gotoEditor,
    );

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(CupertinoIcons.back, color: primaryColor, size: 30),
            onPressed: () => Navigator.of(context).pop(),
          ),
          shadowColor: Colors.transparent,
          backgroundColor: Colors.white,
          title: LocalText(
            context,
            "pickup.info.title",
            fontSize: 20,
            color: primaryColor,
          ),
        ),
        body: Card(
          child: Column(
            children: <Widget>[
              Expanded(
                  child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: ListView(children: <Widget>[
                  pickupNumberBox,
                  pickupDateBox,
                  timeBox,
                  customerBox,
                  _pickup!.completeRemark == null ||
                          _pickup!.completeRemark == ""
                      ? Container()
                      : customerRemarkBox,
                  staffNameBox,
                  statusBox,
                  _pickup!.rescheduleRemark == null ||
                          _pickup!.rescheduleRemark == ""
                      ? Container()
                      : rescheduleRemarkBox,
                  _pickup!.completeRemark == null ||
                          _pickup!.completeRemark == ""
                      ? Container()
                      : completeRemarkBox,
                  deliveryAddressBox,
                  _pickup!.photoUrls.length == 0 ? Container() : img,
                  _pickup!.status == "confirmed" ||
                          _pickup!.status == "rescheduled"
                      ? continueBtn
                      : Container(),
                  SizedBox(
                    height: 20,
                  )
                ]),
              )),
            ],
          ),
        ),
      ),
    );
  }

  _gotoEditor() async {
    bool? deleted = await Navigator.push<bool>(
        context,
        CupertinoPageRoute(
            builder: (context) => PickupEditor(pickup: widget.pickup)));
    if (deleted ?? false) {
      Navigator.pop(context);
    } else {
      Pickup? p = await context.read<PickupModel>().getPickup(_pickup!.id!);
      if (p == null) return;
      initPackage(p);
    }
  }
}
