import 'package:driverapp/core/enum/viewstate.dart';
import 'package:driverapp/core/model/orders/next_order_request_model.dart';
import 'package:driverapp/core/model/orders/pending_orders_res_model.dart';
import 'package:driverapp/core/viewModels/orders_view_model.dart';
import 'package:driverapp/pages/base/base_view.dart';
import 'package:driverapp/pages/delivery/information_page.dart';
import 'package:driverapp/uitl/utils.dart';
import 'package:driverapp/widgets/buttons/secondary_button.dart';
import 'package:driverapp/widgets/delivery/customer_brief_card.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:location/location.dart';

import '../../uitl/translations_delegate_base.dart';
import '../../widgets/others/app_scaffold_widget.dart';

class DeliveryConfirmedPage extends StatelessWidget {
  final PendingOrdersRes item;

  DeliveryConfirmedPage(this.item);

  @override
  Widget build(BuildContext context) {
    return BaseView<OrdersViewModel>(
      builder: (_, model, w) => AppScaffold(
        isShowAppBar: true,
        appBarColor: Theme.of(context).primaryColor,
        arrowColor: Colors.white,
        titleColor: Colors.white,
        body: SafeArea(
          child: Container(
            color: Theme.of(context).primaryColor,
            child: ListView(
              children: <Widget>[
                Stack(
                  children: <Widget>[
                    Column(
                      children: <Widget>[
                        Container(
                          child: Column(
                            children: <Widget>[
                              Container(
                                width: MediaQuery.of(context).size.width,
                                height: MediaQuery.of(context).size.width * 0.7,
                                padding: EdgeInsets.only(
                                  top: MediaQuery.of(context).size.width * 0.12,
                                ),
                                decoration: BoxDecoration(
                                    color: Colors.white10,
                                    shape: BoxShape.circle),
                                child: Column(
                                  children: <Widget>[
                                    Icon(
                                      Icons.check_circle,
                                      color: Colors.white,
                                      size: 75,
                                    ),
                                    SizedBox(
                                      height:
                                          MediaQuery.of(context).size.width *
                                              0.03,
                                    ),
                                    Text(
                                      TranslationBase.of(context).deliveryInfo,
                                      style: TextStyle(
                                          color: Colors.white,
                                          fontSize: 20,
                                          fontWeight: FontWeight.bold),
                                    ),
                                    SizedBox(
                                      height:
                                          MediaQuery.of(context).size.width *
                                              0.01,
                                    ),
                                    Text(
                                      TranslationBase.of(context)
                                          .confirmationSent,
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 13,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
//                    Container(
//                      width: MediaQuery.of(context).size.width,
//                      height: MediaQuery.of(context).size.width,
//                    ),
                    Container(
                      width: MediaQuery.of(context).size.width,
                      height: MediaQuery.of(context).size.width * 1.0,
                      margin: EdgeInsets.only(
                        top: MediaQuery.of(context).size.width * 0.75,
                      ),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(80),
                            topRight: Radius.circular(80)),
                      ),
                      child: Column(
                        mainAxisAlignment: MediaQuery.of(context).orientation ==
                                Orientation.landscape
                            ? MainAxisAlignment.center
                            : MainAxisAlignment.end,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          Container(
                            margin: EdgeInsets.only(
                              bottom: MediaQuery.of(context).size.width * 0.00,
                            ),
                            child: Column(
                              children: <Widget>[
                                FlatButton.icon(
                                  padding: EdgeInsets.all(8),
                                  color: Colors.orangeAccent,
                                  shape: RoundedRectangleBorder(
                                    borderRadius:
                                        new BorderRadius.circular(10.0),
                                  ),
                                  label: Text(
                                    TranslationBase.of(context).addNoteBtn,
                                    style: TextStyle(color: Colors.white),
                                  ),
                                  icon: Icon(
                                    Icons.mode_edit,
                                    color: Colors.white,
                                  ),
                                  onPressed: () {},
                                ),
                                SizedBox(
                                  height: MediaQuery.of(context).size.width *
                                      0.02, //20,
                                ),
                                Container(
                                  margin: EdgeInsets.all(10),
                                  child: SecondaryButton(
                                    label: TranslationBase.of(context)
                                        .nextDelivery,
                                    loading: model.state == ViewState.BusyLocal,
                                    onTap: () {
                                      getNextOrder(context, model);
                                    },
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(
                        top: MediaQuery.of(context).size.width * 0.6,
                      ),
                      child: CustomerBriefCard(
                          itemId: item.orderID,
                          customerFirstName: item.firstName,
                          customerLastName: item.lastName,
                          mobileNo: item.mobileNumber,
                          totalPayment: item.amount,
                          showDistance: false,
                          deliveryTime: item.orderCreatedOn),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  getNextOrder(BuildContext context, OrdersViewModel model) async {
    model.setState(ViewState.BusyLocal);
    LocationData loc = await Utils.getLocation();
    NextOrderRequestModel nextOrderRequestModel = NextOrderRequestModel(
        pageIndex: 0,
        pageSize: 0,
        latitude: loc.latitude.toString(),
        //"46.621730",
        longitude: loc.longitude.toString(),
        //"24.797682",
        searchKey: "");
    await model.nextOrder(nextOrderRequestModel);
    if (model.state == ViewState.ErrorLocal) {
      Utils.showErrorToast(model.error);
    } else {
      if (model.nextOrdersList != null && model.nextOrdersList.length == 0) {
        Utils.showErrorToast("No Items in the list");
      } else {
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => InformationPage(model.nextOrdersList[0]),
          ),
        );
      }
    }
  }
}
