import 'dart:convert';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:get/get.dart';
//import 'package:google_maps_webservice/directions.dart';
import 'package:http/http.dart' as http;
import 'package:gifunavi/model/destination.dart';
import 'package:gifunavi/pages/destination/destination_controller.dart';

import '../utils/const.dart';

class DestinationService {
  static Future<List<dynamic>> getDestinations(int userId) async {
    List<dynamic> cats = [];
    String serverUrl = ConstValues.currentServer();
    String url = "$serverUrl/api/destinations/?user_id=$userId";
    //print('++++++++$url');
    final http.Response response =
        await http.get(Uri.parse(url), headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    });

    if (response.statusCode == 200) {
      cats = json.decode(utf8.decode(response.bodyBytes));
    }
    return cats;
  }

  static Future<Map<String, dynamic>> deleteDestination(int destId) async {
    Map<String, dynamic> cats = {};
    String serverUrl = ConstValues.currentServer();
    String url = "$serverUrl/api/delete_destination/?dest_id=$destId";
    //print('++++++++$url');
    final http.Response response =
        await http.get(Uri.parse(url), headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    });

    if (response.statusCode == 200) {
      cats = json.decode(utf8.decode(response.bodyBytes));
    }
    //print("####### ---- $cats");
    return cats;
  }

  static Future<int> updateOrder(int actionId, int order, String dir) async {
    int cats = 0;
    String serverUrl = ConstValues.currentServer();
    String url =
        "$serverUrl/api/updateorder/?user_action_id=$actionId&order=$order&dir=$dir";
    //print('++++++++$url');
    final http.Response response =
        await http.get(Uri.parse(url), headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    });

    if (response.statusCode == 200) {
      cats = json.decode(utf8.decode(response.bodyBytes));
    }
    return cats;
  }

  static Future<List<PointLatLng>>? getDestinationLine(
      List<Destination> destinations, Map<String, dynamic> mtx) async {
    PolylinePoints polylinePoints = PolylinePoints();

    if (destinations.isEmpty) {
      return [];
    }

    //print("##### @@@@@ ${destinations[0].lat}");
    PointLatLng origin =
        PointLatLng(destinations[0].lat!, destinations[0].lon!);
    PointLatLng dest = PointLatLng(destinations[destinations.length - 1].lat!,
        destinations[destinations.length - 1].lon!);

    List<PolylineWayPoint> wayPoints = [];
    int i = 0;
    for (dynamic d in destinations) {
      if (i == 0 || i == (destinations.length - 1)) {
        i += 1;
        continue;
      }
      double la = d.lat;
      double ln = d.lon;

      int j = 0;

      PolylineWayPoint pwp =
          PolylineWayPoint(location: "$la,$ln", stopOver: false);

      //print("----- UUUUUU ${pwp}");
      //PointLatLng wp = PointLatLng(d["Location"]["geometry"][0][1], d["Location"]["geometry"][0][0]);
      wayPoints.add(pwp);
      i += 1;
      j += 4;
    }

    final DestinationController destinationController =
        Get.find<DestinationController>();
    // int travMode = destinationController.travelMode.value;
    // String mode = "WALKING";
    // if (travMode == 1) {
    //   //_mode = TravelMode.driving;
    //   mode = "DRIVING";
    // } else if (travMode == 2) {
    //   //_mode = TravelMode.transit;
    //   mode = "TRANSIT";
    // }

    //PolylineResult result = await polylinePoints.getRouteBetweenCoordinates("AIzaSyAUBI1ablMKuJwGj2-kSuEhvYxvB1A-mOE", PointLatLng(35.389282, 136.498027), PointLatLng(36.285848, 137.575186));
    Map<String, dynamic> pl =
        destinationController.matrix["routes"][0]["overview_polyline"];
    List<PointLatLng> result = polylinePoints.decodePolyline(pl["points"]);
    //List<PointLatLng> result = polylinePoints.decodePolyline("qkyvEq`z`Yp@DBMr@XL@Td@Eb@PREd@IFe@rKIzCY|GEvCBzCHvS@xC?HnBHtBHlBFnBFhGRtDVW~BE`@ICHLk@dE_ClPgAtHu@bFsAhPg@~Ge@bFaEtg@kEpi@oCd\\w@nIw@hGe@fBy@nBqAjC{@zBgBtFOd@M@Wv@i@`BQf@ITKCuE`@yDZqBRCHa@DKG_AHwBRiBR_Fp@y@LYBY]M@KJo@v@M@cAGoGN_Cx@}Cf@}@@mM~@qF`@gCLwBj@sBrAeAhAsAtCoF|MmAbD{@fBwAdBw@p@_Ax@BFOHAl@?`@MAQCEAOIQSaBx@{Ah@eATsAHSB?d@A`D?f@IdWy@AS??V?|BCJ}@?cA?k@Au@wBqDuKQaACg@z@gELg@GK~@uEp@{A@q@y@CHwFHcG?KDqCDK^ABABEH{AE{B{@_Ho@uFIaBFQhBaC@SQSg@k@g@q@Yw@qA{De@}B]uDCsAMEWDqAFu@@^A@TDjA@vDA`CETK|AEtAIFY@o@ALpBZ~HBlCBn@EDGPu@pASJO`@Qf@?ROr@K?qDLHnEUTsDNkENYB{Ab@I^?zA}CrCkBfBw@t@@LwA`Bo@r@eGvD}BrAGGuAj@[?i@rBVi@P}@T?F?^MxDuBhDsBzAcAn@s@zCgDAI~A{A|CsC?{A?UHItA_@DCXC~J_@TUIoEvDKTm@?Y^iALIb@k@f@aAE}AA_BC{@\\Cv@CxAEj@ExCwDDc@CYFANCh@WHEIIRQhB}B|C_E\\w@Hq@JE?a@O}CGkAIwEGmDEmDAKLA^?A}@C{@?e@E_DFQNi@LcB\\eBPsADGKOEWBOH[GCPs@Pq@\\cANs@^q@jAu@fCqAf@]HCXoCV_BVmAZmBVcDBeCCgDAaB?s@RE?aCCaEAyHAoDd@EJiD@_@AaAj@A\\A?Gp@@r@oBXm@LQ?IEy@Fy@tA[n@Gj@Tz@[~ACdAAx@Lp@Kr@]hAa@HAQoCMwCSwGSiGK_CCCKaBCgCOoCOgECwGB_OB{JHkBEmC?yCDyFF{QFue@BsYByE?oAEgAByLBiL?gLBuGXsEd@cCNA?OHa@jAuCn@eAtCyDh@k@v@EvBKr@EEkACUKaC?G~@gAlCeDFBT[jFeGZAfBEh@UpBM`AEMaFjFYIhE?hEPpCJzAPt@Fj@GNUFu@N[FyBbAuB`@_@LEIOB}@HUBQFk@FcAACGQA}@Bi@F@F[Dc@D[FQHELGhBMtDGR?D");
    //PolylineResult result = await polylinePoints.getRouteBetweenCoordinates("AIzaSyAUBI1ablMKuJwGj2-kSuEhvYxvB1A-mOE", origin,dest, travelMode: _mode, wayPoints: wayPoints);

    //print("#####@@@@@ ${result.points}");
    return result;
  }
}
