import 'package:barcode_scan/platform_wrapper.dart';
import 'package:driverapp/core/enum/viewstate.dart';
import 'package:driverapp/core/viewModels/orders_view_model.dart';
import 'package:driverapp/pages/delivery/information_page.dart';
import 'package:driverapp/pages/orders/pending_orders_page.dart';
import 'package:driverapp/uitl/app_toast.dart';
import 'package:driverapp/uitl/utils.dart';
import 'package:driverapp/widgets/others/app_scaffold_widget.dart';
import 'package:driverapp/widgets/others/network_base_view.dart';
import 'package:driverapp/widgets/others/rounded_container.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../base/base_view.dart';

class DashboardScreen extends StatefulWidget {
  @override
  _DashboardScreenState createState() => _DashboardScreenState();
}

class _DashboardScreenState extends State<DashboardScreen> {
  @override
  Widget build(BuildContext context) {
    int orderId;
    return BaseView<OrdersViewModel>(
      onModelReady: (model) => model.getPendingOrders(),
      builder: (BuildContext context, OrdersViewModel model, Widget child) =>
          AppScaffold(
        body: Column(
          // mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.all(16.0),
                  child: Column(
                    children: <Widget>[
                      SafeArea(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Text(
                              'Have a great day,',
                              style: TextStyle(
                                  fontSize: 14.5,
                                  color: Color(0xff636363),
                                  fontWeight: FontWeight.w300),
                            ),
                            Padding(
                              padding: EdgeInsets.only(top: 4.5),
                              child: Text(
                                'Driver Name',
                                style: TextStyle(
                                    fontWeight: FontWeight.w400,
                                    fontSize: 25.0),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Padding(
                  padding: EdgeInsets.all(16.0),
                  child: SafeArea(
                    child: Column(
                      children: <Widget>[
                        CircleAvatar(
                          radius: 25.5,
                          backgroundColor: Color(0xff30B7B9),
                          child: CircleAvatar(
                            backgroundColor: Color(0xff30B7B9),
                            maxRadius: 26.0,
                            child: Image.asset(
                              'assets/images/driver.png',
                              fit: BoxFit.contain,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: 10.0),
                        child: Container(
                          height: MediaQuery
                              .of(context)
                              .size
                              .height * 0.16,
                          width: MediaQuery
                              .of(context)
                              .size
                              .width * 0.44,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(15.0),
                            gradient: LinearGradient(
                                colors: [Color(0xff17AFB8), Color(0xff49C1BC)]),
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Expanded(
                                child: Padding(
                                  padding: EdgeInsets.all(2.0),
                                child: Column(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceEvenly,
                                    children: <Widget>[
                                      Text(
                                        'You Have',
                                        style: TextStyle(
                                            color: Colors.white,
                                            fontSize: 10.0),
                                      ),
                                      Text(
                                        '5',
                                        style: TextStyle(
                                            color: Colors.white,
                                            fontSize: 25.0),
                                      ),
                                      Text(
                                        'Undeliverd\nPackages',
                                        style: TextStyle(
                                            color: Colors.white, fontSize: 9.5),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(right: 9.5),
                                child: Container(
                                  width: 110,
                                  height: 110,
                                  decoration: BoxDecoration(
                                      color: Colors.white10,
                                      shape: BoxShape.circle),
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: <Widget>[
                                       Image.asset(
                                            'assets/images/closed_box.png',
                                            height: MediaQuery
                                                .of(context)
                                                .size
                                                .height *
                                                0.10,
                                            width: MediaQuery
                                                .of(context)
                                                .size
                                                .width *
                                                0.20,
                                        scale: 0.9,
                                        fit: BoxFit.cover,
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                Expanded(
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: 10.0),
                        child: Container(
                          height: MediaQuery.of(context).size.height * 0.16,
                        width: MediaQuery
                            .of(context)
                            .size
                            .width * 0.45,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(15.0),
                            gradient: LinearGradient(
                                colors: [Color(0xff17AFB8), Color(0xff49C1BC)]),
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Expanded(
                                child: Padding(
                                  padding: EdgeInsets.all(2.0),
                                child: Column(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceEvenly,
                                  children: <Widget>[
                                    Text(
                                      'You Have',
                                      style: TextStyle(
                                          color: Colors.white, fontSize: 10.0),
                                    ),
                                    Text(
                                      '25',
                                      style: TextStyle(
                                          color: Colors.white, fontSize: 25.0),
                                    ),
                                     Text(
                                        'Succesful\nDeliveries',
                                        style: TextStyle(
                                            color: Colors.white,
                                            fontSize: 10.0),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(right: 9.5),
                                child: Container(
                                  width: 110,
                                  height: 110,
                                  decoration: BoxDecoration(
                                      color: Colors.white10,
                                      shape: BoxShape.circle),
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: <Widget>[
                                      Image.asset(
                                        'assets/images/opend_box.png',
                                        height:
                                            MediaQuery.of(context).size.height *
                                                0.10,
                                        width:
                                            MediaQuery.of(context).size.width *
                                                0.20,
                                        scale: 0.9,
                                        fit: BoxFit.cover,
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
            Padding(
              padding: EdgeInsets.symmetric(vertical: 16.0, horizontal: 15.0),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: InkWell(
                      child: Container(
                        height: MediaQuery
                            .of(context)
                            .size
                            .height * 0.18,
                        width: MediaQuery
                            .of(context)
                            .size
                            .width * 0.50,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(15.0),
                            gradient: LinearGradient(colors: [
                              Color(0xff48C0BC),
                              Color(0xff17AFB8)
                            ])),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Image.asset(
                                  'assets/images/qr_code.png',
                                  width:
                                  MediaQuery
                                      .of(context)
                                      .size
                                      .width * 0.26,
                                  height:
                                  MediaQuery
                                      .of(context)
                                      .size
                                      .height * 0.14,
                                  fit: BoxFit.contain,
                                )
                              ],
                            ),
                            Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                InkWell(
                                  onTap: () {
                                    _scanQrAndGetPatient(context, model);
                                  },
                                  child: Text(
                                    'SCAN',
                                    style: TextStyle(
                                        fontSize: 35.0,
                                        color: Colors.white,
                                        fontWeight: FontWeight.w400),
                                  ),
                                ),
                                Padding(
                                  padding: EdgeInsets.only(top: 6.0),
                                  child: Text(
                                    'To add package to que ',
                                    style: TextStyle(
                                      fontSize: 12.0,
                                      color: Colors.white,
                                      letterSpacing: 0.5,
                                      wordSpacing: 5.5,
                                    ),
                                  ),
                                ),
                              ],
                            )
                          ],
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 1.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Column(
                    children: <Widget>[
                      Text(
                        'Nearest Drop-Offs',
                        style: TextStyle(
                          fontSize: 21.0,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ],
                  ),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      InkWell(
                          child: Row(
                            children: <Widget>[
                              Text(
                                'See All',
                                style: TextStyle(
                                    fontSize: 14.5, color: Color(0xff42B6AD)),
                              ),
                              Icon(
                                Icons.arrow_forward_ios,
                                size: 15.0,
                                color: Color(0xff42B6AD),
                              ),
                            ],
                          ),
                          onTap: () => Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => OrdersListScreen()),
                              )),
                    ],
                  ),
                ],
              ),
            ),
            NetworkBaseView(
              baseViewModel: model,
              child: Expanded(
                child: ListView.builder(
                  shrinkWrap: true,
                  scrollDirection: Axis.vertical,
                  itemCount: //model.orders == null ? 0 : model.orders.length,
                      3,
                  itemBuilder: (BuildContext context, int index) {
                    return Padding(
                      padding: EdgeInsets.symmetric(horizontal: 12.2),
                      child: InkWell(
                        child: RoundedContainer(
                          height: MediaQuery.of(context).size.height * 0.109,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Expanded(
                                flex: 1,
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Padding(
                                      padding: EdgeInsets.only(
                                          left: 10.0, bottom: 5.0),
                                      child: Image.asset(
                                        'assets/images/location.png',
                                        height:
                                            MediaQuery.of(context).size.height *
                                                0.06,
                                        width:
                                            MediaQuery.of(context).size.width *
                                                0.05,
                                      ),
                                    )
                                  ],
                                ),
                              ),
                              if (model.orders.length != 0)
                                Expanded(
                                  flex: 3,
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: <Widget>[
                                      Padding(
                                        padding: EdgeInsets.only(top: 5.0),
                                        child: Text(
                                          model.orders[index].firstName +
                                              ' ' +
                                              model.orders[index].lastName,
                                          style: TextStyle(fontSize: 18.0),
                                        ),
                                      ),
                                      Text(
                                        model.orders[index].mobileNumber,
                                        style: TextStyle(
                                          color: Color(0xff30B7B9),
                                          fontWeight: FontWeight.w600,
                                          fontSize: 15.0,
                                        ),
                                      ),
                                      Expanded(
                                        child: Text(
                                          'Olaya ST, Behind kfc next to king , ',
                                          style:
                                              TextStyle(color: Colors.black45),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              Padding(
                                padding: EdgeInsets.all(10.0),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Expanded(
                                      child: CircleAvatar(
                                        backgroundColor: Colors.black45,
                                        radius: 28.0,
                                        child: CircleAvatar(
                                          backgroundColor: Colors.white,
                                          maxRadius: 25.1,
                                          child: Padding(
                                            padding: const EdgeInsets.all(8.0),
                                            child: Text(
                                              '3 K.m \n away',
                                              style: TextStyle(
                                                  color: Color(0xff30B7B9),
                                                  fontSize: 10.5,
                                                  fontWeight: FontWeight.w400),
                                            ),
                                          ),
                                        ),
                                      ),
                                    )
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                        onTap: () {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) =>
                                      InformationPage(model.orders[index])));
                        },
                      ),
                    );
                  },
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  _scanQrAndGetPatient(BuildContext context, OrdersViewModel model) async {
    /// When give qr we will change this method to get data
    /// var result = await BarcodeScanner.scan();
    /// int patientID = get from qr result
    var result = await BarcodeScanner.scan();
    //  if (result.rawContent == "") {
    List<String> listOfParams = result.rawContent.split(',');
    String patientType = "1";
    await model.insertOrder();
    if (model.state == ViewState.ErrorLocal) {
      Utils.showErrorToast(model.error);
    } else {
      AppToast.showSuccessToast(message: "Order Added");
    }
  }
}
