import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class TankerOptionsDemo extends StatefulWidget {

  @override
  State<TankerOptionsDemo> createState() => _TankerOptionsDemoState();

}

class _TankerOptionsDemoState extends State<TankerOptionsDemo> {

  GoogleMapController? mapController;

  LatLng customerLocation =
  LatLng(17.3850,78.4867);

  String selectedTanker="TNK1";

  List tankers=[

    {
      "id":"TNK1",
      "lat":17.387,
      "lng":78.489,
      "distance":2.5,
      "eta":15,
      "status":"AVAILABLE"
    },

    {
      "id":"TNK2",
      "lat":17.381,
      "lng":78.480,
      "distance":5,
      "eta":30,
      "status":"ON_DELIVERY",
      "availableIn":20
    },

    {
      "id":"TNK3",
      "lat":17.370,
      "lng":78.470,
      "distance":9,
      "eta":50,
      "status":"FAR"
    }

  ];

  Set<Marker> markers={};

  @override
  void initState(){

    super.initState();

    createMarkers();

  }

  void createMarkers(){

    markers.add(

        Marker(

            markerId: MarkerId("customer"),

            position: customerLocation,

            infoWindow:
            InfoWindow(title:"Delivery Location"),

            icon: BitmapDescriptor.defaultMarkerWithHue(
                BitmapDescriptor.hueBlue)

        )

    );

    for(var t in tankers){

      BitmapDescriptor color;

      if(t["status"]=="AVAILABLE"){

        color=
            BitmapDescriptor.defaultMarkerWithHue(
                BitmapDescriptor.hueGreen);

      }

      else if(t["status"]=="ON_DELIVERY"){

        color=
            BitmapDescriptor.defaultMarkerWithHue(
                BitmapDescriptor.hueOrange);

      }

      else{

        color=
            BitmapDescriptor.defaultMarkerWithHue(
                BitmapDescriptor.hueRed);

      }

      markers.add(

          Marker(

              markerId: MarkerId(t["id"]),

              position: LatLng(
                  t["lat"],
                  t["lng"]
              ),

              infoWindow:
              InfoWindow(

                  title:t["id"],

                  snippet:
                  "ETA ${t["eta"]} min"

              ),

              icon:color

          )

      );

    }

    setState(() {});

  }

  @override
  Widget build(BuildContext context){

    return Scaffold(

      appBar: AppBar(

        title:
        Text("Select Tanker"),

        backgroundColor:
        Colors.white,

      ),

      body:

      Column(

        children:[

          Container(

            height:300,

            child:

            GoogleMap(

              initialCameraPosition:

              CameraPosition(

                  target:
                  customerLocation,

                  zoom:13

              ),

              markers:markers,

            ),

          ),

          Expanded(

              child:

              ListView(

                children:

                tankers.map((t){

                  return tankerCard(t);

                }).toList(),

              )

          )

        ],

      ),

      bottomNavigationBar:

      Container(

        padding:EdgeInsets.all(12),

        child:

        ElevatedButton(

            style:

            ElevatedButton.styleFrom(

                backgroundColor:
                Colors.green

            ),

            onPressed:(){

              Navigator.pop(context);

            },

            child:

            Text("Confirm Selection")

        ),

      ),

    );

  }

  Widget tankerCard(var tanker){

    Color color;

    if(tanker["status"]=="AVAILABLE"){

      color=Colors.green;

    }

    else if(tanker["status"]=="ON_DELIVERY"){

      color=Colors.orange;

    }

    else{

      color=Colors.red;

    }

    return GestureDetector(

      onTap:(){

        selectedTanker=tanker["id"];

        setState(() {});

      },

      child:

      Container(

        margin:
        EdgeInsets.all(10),

        padding:
        EdgeInsets.all(12),

        decoration:

        BoxDecoration(

          borderRadius:
          BorderRadius.circular(10),

          border:

          Border.all(

              color:

              selectedTanker==tanker["id"]

                  ?

              Colors.green

                  :

              Colors.grey

          ),

        ),

        child:

        Column(

          crossAxisAlignment:
          CrossAxisAlignment.start,

          children:[

            Row(

              children:[

                Text(

                  tanker["id"],

                  style:

                  TextStyle(

                      fontSize:18,

                      fontWeight:
                      FontWeight.bold

                  ),

                ),

                Spacer(),

                Container(

                  padding:
                  EdgeInsets.all(6),

                  decoration:

                  BoxDecoration(

                      color:
                      color,

                      borderRadius:
                      BorderRadius.circular(5)

                  ),

                  child:

                  Text(

                      tanker["status"],

                      style:

                      TextStyle(
                          color:Colors.white)

                  ),

                )

              ],

            ),

            SizedBox(height:8),

            Text(
                "Distance : ${tanker["distance"]} km"),

            Text(
                "ETA : ${tanker["eta"]} min"),

            if(tanker["status"]=="ON_DELIVERY")

              Text(
                  "Available in ${tanker["availableIn"]} min"),

            if(tanker["status"]=="FAR")

              Text(
                  "⚠ Transport charges higher"),

            SizedBox(height:6),

            if(tanker["status"]=="AVAILABLE")

              Text(
                  "BEST OPTION",

                  style:

                  TextStyle(
                      color:Colors.green)

              )

          ],

        ),

      ),

    );

  }

}