import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:supplier_new/common/settings.dart';
import '../resources/drivers_model.dart';
import '../resources/tankers_model.dart';
import '../resources/source_loctaions_model.dart';

class AssignTankerScreen extends StatefulWidget {

  final order;
  final List<DriversModel> driversList;
  final List<TankersModel> tankersList;
  final List<SourceLocationsModel> sourceLocationsList;

  AssignTankerScreen({
    this.order,
    required this.driversList,
    required this.tankersList,
    required this.sourceLocationsList,
  });

  @override
  State<AssignTankerScreen> createState() =>
      _AssignTankerScreenState();
}

class _AssignTankerScreenState
    extends State<AssignTankerScreen> {

  int? selectedTankerIndex;
  int? selectedDriverIndex;
  int? selectedSourceIndex;

  int _capToLiters(dynamic cap){

    if(cap==null)return -1;

    if(cap is num){
      return cap.round();
    }

    final s=cap
        .toString()
        .toLowerCase()
        .replaceAll(',','')
        .trim();

    final match=
    RegExp(r'(\d+(\.\d+)?)')
        .firstMatch(s);

    if(match==null)return -1;

    final n=
        double.tryParse(match.group(1)!)??-1;

    if(n<0)return -1;

    if(s.contains('kl')){
      return (n*1000).round();
    }

    return n.round();

  }

  DateTime parseOrderDateTime(){

    DateTime d=
    DateFormat("dd-MMM-yyyy")
        .parse(widget.order.date);

    DateTime t=
    DateFormat("hh:mm a")
        .parse(widget.order.time);

    return DateTime(
        d.year,
        d.month,
        d.day,
        t.hour,
        t.minute
    );

  }

  bool isTankerBlocked(
      TankersModel tanker){

    if(tanker.blocked_dates==null ||
        tanker.blocked_dates.isEmpty){
      return false;
    }

    DateTime orderDT=
    parseOrderDateTime();

    for(var slot
    in tanker.blocked_dates){

      if(slot['date']!=
          widget.order.date){
        continue;
      }

      String timeRange=
      slot['time'];

      List parts=
      timeRange.split("to");

      if(parts.length!=2){
        continue;
      }

      DateTime start=
      DateFormat("hh:mm a")
          .parse(parts[0].trim());

      DateTime end=
      DateFormat("hh:mm a")
          .parse(parts[1].trim());

      DateTime startDT=
      DateTime(
          orderDT.year,
          orderDT.month,
          orderDT.day,
          start.hour,
          start.minute
      );

      DateTime endDT=
      DateTime(
          orderDT.year,
          orderDT.month,
          orderDT.day,
          end.hour,
          end.minute
      );

      if(orderDT.isAfter(startDT)
          &&
          orderDT.isBefore(endDT)){
        return true;
      }

      if(orderDT==startDT ||
          orderDT==endDT){
        return true;
      }

    }

    return false;

  }

  String? getDriverBlockedTime(
      DriversModel driver){

    if(driver.blocked_dates==null ||
        driver.blocked_dates.isEmpty){
      return null;
    }

    DateTime orderDT=
    parseOrderDateTime();

    for(var slot
    in driver.blocked_dates){

      if(slot['date']!=
          widget.order.date){
        continue;
      }

      String timeRange=
      slot['time'];

      List parts=
      timeRange.split("to");

      if(parts.length!=2){
        continue;
      }

      DateTime start=
      DateFormat("hh:mm a")
          .parse(parts[0].trim());

      DateTime end=
      DateFormat("hh:mm a")
          .parse(parts[1].trim());

      DateTime startDT=
      DateTime(
          orderDT.year,
          orderDT.month,
          orderDT.day,
          start.hour,
          start.minute
      );

      DateTime endDT=
      DateTime(
          orderDT.year,
          orderDT.month,
          orderDT.day,
          end.hour,
          end.minute
      );

      if(orderDT.isAfter(startDT)
          &&
          orderDT.isBefore(endDT)){
        return timeRange;
      }

      if(orderDT==startDT ||
          orderDT==endDT){
        return timeRange;
      }

    }

    return null;

  }

  @override
  Widget build(BuildContext context){

    return Scaffold(

      backgroundColor: Colors.white,

      appBar: AppBar(

        title:
        Text("Assign Tanker"),

      ),

      body:

      Column(

        children:[

          Expanded(

            child:

            SingleChildScrollView(

              padding:
              EdgeInsets.all(16),

              child:

              Column(

                crossAxisAlignment:
                CrossAxisAlignment.start,

                children:[

                  /// ORDER CARD

                  Container(

                    padding:
                    EdgeInsets.all(12),

                    decoration:
                    BoxDecoration(

                      borderRadius:
                      BorderRadius.circular(12),

                      border:
                      Border.all(
                          color:
                          Color(0XFFC9C2F0)
                      ),

                    ),

                    child:

                    Row(

                      children:[

                        Column(

                          crossAxisAlignment:
                          CrossAxisAlignment.start,

                          children:[

                            Text(
                                widget.order.building_name
                            ),

                            Text(
                                widget.order.displayAddress
                            ),

                          ],

                        ),

                        Spacer(),

                        Text(
                            "${widget.order.distanceInKm} Km"
                        ),

                      ],

                    ),

                  ),

                  SizedBox(height:20),

                  /// TANKERS

                  Text("SELECT TANKER"),

                  SizedBox(height:10),

                  ...widget.tankersList
                      .where((t)=>

                  _capToLiters(t.capacity)==
                      _capToLiters(widget.order.capacity)

                  ).toList()
                      .asMap()
                      .entries
                      .map((entry){

                    int idx=
                        entry.key;

                    var d=
                        entry.value;

                    bool blocked=
                    isTankerBlocked(d);

                    return GestureDetector(

                      onTap:blocked?null:(){

                        setState(() {

                          selectedTankerIndex=
                              idx;

                          selectedDriverIndex=
                          null;

                        });

                      },

                      child:

                      Card(

                        child:

                        Padding(

                          padding:
                          EdgeInsets.all(12),

                          child:

                          Text(
                              d.tanker_name
                          ),

                        ),

                      ),

                    );

                  }).toList(),

                ],

              ),

            ),

          ),

          /// ASSIGN BUTTON

          Padding(

            padding:
            EdgeInsets.all(16),

            child:

            SizedBox(

              width:double.infinity,

              child:

              ElevatedButton(

                style:
                ElevatedButton.styleFrom(

                    backgroundColor:
                    Color(0XFF8270DB)

                ),

                onPressed:()async{

                  if(selectedTankerIndex==null){

                    AppSettings
                        .longFailedToast(
                        "Select tanker"
                    );

                    return;

                  }

                  final selectedTanker=
                  widget.tankersList[
                  selectedTankerIndex!
                  ];

                  final selectedDriver=
                  selectedDriverIndex!=null
                      ?
                  widget.driversList[
                  selectedDriverIndex!
                  ]
                      :
                  null;

                  final selectedSource=
                  selectedSourceIndex!=null
                      ?
                  widget.sourceLocationsList[
                  selectedSourceIndex!
                  ]
                      :
                  null;

                  var payload={};

                  payload["tankerName"]=
                      selectedTanker.tanker_name;

                  payload["delivery_agent"]=
                      selectedDriver?.driver_name;

                  payload["delivery_agent_mobile"]=
                      selectedDriver?.phone_number;

                  payload["water_source_location"]=
                      selectedSource?.source_name;

                  bool status=
                  await AppSettings
                      .assignTanker(
                      payload,
                      widget.order.dbId
                  );

                  if(status){

                    AppSettings
                        .longSuccessToast(
                        "Assigned"
                    );

                    Navigator.pop(
                        context,
                        true
                    );

                  }

                },

                child:

                Text("Assign"),

              ),

            ),

          )

        ],

      ),

    );

  }

}