import 'package:driverapp/config/config.dart';
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/service/orders_service.dart';
import 'package:driverapp/core/viewModels/orders_view_model.dart';
import 'package:driverapp/locator.dart';
import 'package:driverapp/pages/base/base_view.dart';
import 'package:driverapp/root_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';
import 'information_page.dart';

class DeliveryConfirmedPage extends StatelessWidget {
  final PendingOrdersRes item;

  DeliveryConfirmedPage(this.item);

  @override
  Widget build(BuildContext context) {
    return BaseView<OrdersViewModel>(
      builder: (_, model, w) => AppScaffold(
        isAppBarGradient: true,
        isShowAppBar: true,
        appBarColor: Theme.of(context).primaryColor,
        arrowColor: Colors.white,
        titleColor: Colors.white,
        body: SafeArea(
          child: Container(
            //
            child: ListView(
              children: <Widget>[
                Stack(
                  children: <Widget>[
                    Container(
                      decoration: BoxDecoration(gradient: LINEAR_GRADIENT),
                      height: MediaQuery.of(context).orientation ==
                              Orientation.portrait
                          ? MediaQuery.of(context).size.width * 1
                          : MediaQuery.of(context).size.width * 1,
                      child: Stack(
                        children: [
                          Container(
                            width: MediaQuery.of(context).size.width,
                            height: MediaQuery.of(context).orientation ==
                                    Orientation.portrait
                                ? MediaQuery.of(context).size.width * 0.7
                                : MediaQuery.of(context).size.width * 0.5,
                            padding: EdgeInsets.only(
                              top: MediaQuery.of(context).size.width * 0.14,
                            ),
                            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(
                                  'Delivery Confirmed',
                                  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).orientation ==
                              Orientation.landscape
                          ? MediaQuery.of(context).size.width * 0.6
                          : MediaQuery.of(context).size.width * 1.0,
                      margin: EdgeInsets.only(
                        top: MediaQuery.of(context).orientation ==
                                Orientation.portrait
                            ? MediaQuery.of(context).size.width * 0.70
                            : MediaQuery.of(context).size.width * 0.60,
                      ),
                      decoration: BoxDecoration(
                        color: Theme
                            .of(context)
                            .scaffoldBackgroundColor,
                        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: Padding(
                              padding: MediaQuery.of(context).orientation ==
                                      Orientation.landscape
                                  ? EdgeInsets.only(top: 60.0)
                                  : EdgeInsets.only(top: 30.0),
                              child: Column(
                                children: <Widget>[
                                  // TODO return add note when its needed
                                  // FlatButton.icon(
                                  //   padding: EdgeInsets.all(14.0),
                                  //   color: Colors.orangeAccent,
                                  //   shape: RoundedRectangleBorder(
                                  //     borderRadius: BorderRadius.circular(10.0),
                                  //   ),
                                  //   label: Text(
                                  //     TranslationBase.of(context).addNoteBtn,
                                  //     style: TextStyle(
                                  //         color: Colors.white, fontSize: 20.0),
                                  //   ),
                                  //   icon: Icon(
                                  //     Icons.mode_edit,
                                  //     color: Colors.white,
                                  //   ),
                                  //   onPressed: () {},
                                  // ),
                                  // SizedBox(
                                  //   height: MediaQuery.of(context).size.width *
                                  //       0.00, //20,
                                  // ),
                                  Padding(
                                    padding: const EdgeInsets.only(top: 30.0),
                                    child: 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).orientation ==
                                Orientation.portrait
                            ? MediaQuery.of(context).size.width * 0.6
                            : MediaQuery.of(context).size.width * 0.4,
                      ),
                      child: CustomerBriefCard(
                          customerOrderId: item.orderID,
                          pharmacyOrderId: item.ePharmacyOrderNo,
                          customerFirstName: item.firstName,
                          customerLastName: item.lastName,
                          mobileNo: item.mobileNumber,
                          totalPayment: item.amount,
                          distanceInKilometers: item.distanceInKilometers,
                          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");
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => RootPage(),
          ),
        );
      } else {
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) =>
                InformationPage(item: model.nextOrdersList[0]),
          ),
        );
      }
    }
  }
}
