package com.awesomeproject3;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.facebook.react.ReactActivity;
import com.facebook.react.bridge.Promise;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.tplmaps3d.CameraPosition;
import com.tplmaps3d.IconFactory;
import com.tplmaps3d.LngLat;
import com.tplmaps3d.MapController;
import com.tplmaps3d.MapView;
import com.tplmaps3d.Marker;
import com.tplmaps3d.MarkerOptions;
import com.tplmaps3d.sdk.model.IconSize;


public class MyFragment extends ReactActivity implements MapView.OnMapReadyCallback, ModuleCallback {
    CustomView customView;
    private MapView mapView;
    MapController _mapController = null;
    TplMapsController tplMapsController;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragmnet_map);
        mapView  = findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        tplMapsController = new TplMapsController(this);
        mapView.loadMapAsync(this);

    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
//        super.onCreateView(inflater, parent, savedInstanceState);
//
//        View rootView = inflater.inflate(R.layout.fragmnet_map, parent, false);
//        mapView  = rootView.findViewById(R.id.map);
//        return rootView;
//    }
//
//
//    @Override
//    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState);
//        mapView.onCreate(savedInstanceState);
//        tplMapsController = new TplMapsController(this);
//
//        try {
//            mapView.loadMapAsync(this);
//        }catch (Exception e){
//            Log.d("ERRORCHEKC" , e.toString());
//        }
//
//    }

    @Override
    public void onPause() {
        super.onPause();
        // do any logic that should happen in an `onPause` method
        // e.g.: customView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
      //  Toast.makeText(getActivity(), "Tada bichu", Toast.LENGTH_SHORT).show();
        // do any logic that should happen in an `onResume` method
        // e.g.: customView.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // do any logic that should happen in an `onDestroy` method
        // e.g.: customView.onDestroy();
    }

    @Override
    public void onMapReady(MapController mapController) {
        _mapController = mapController;


        mapController.setOnCameraChangeEndListener(new MapController.OnCameraChangeEndListener() {
            @Override
            public void onCameraChangeEnd(CameraPosition cameraPosition) {
                if (Constant.promise != null){
                    Constant.promise.resolve(cameraPosition.getPosition().latitude+";"+cameraPosition.getPosition().longitude);
                }

            }
        });

        mapController.setOnMarkerClickListener(new MapController.OnMarkerClickListener() {
            @Override
            public void onMarkerClick(Marker marker) {
                if (Constant.promise != null){
                    Constant.promise.resolve(marker.getPosition().latitude+";"+marker.getPosition().longitude);
                }
            }
        });

    }

    public void zoomCurrentLoc(){
        if (_mapController != null){
            _mapController.setMyLocationEnabled(true, MapController.MyLocationArg.ZOOM_LOCATION_ON_FIRST_FIX);
        }

    }


    private void  addMarker(LngLat lngLat) {
      //  LngLat coord = new LngLat(longitude, latitude);
        Marker marker = _mapController.addMarker(new MarkerOptions().position(lngLat).title("My Location"));

    }


    private void  addCustomMarker(LngLat lngLat) {

        Marker marker = _mapController.addMarker(new MarkerOptions().position(lngLat).title("Custom Marker"));
        marker.setIcon(IconFactory.fromResource(R.drawable.custom, new IconSize(50 , 50)));

    }


    private void setCameraAnimate(double lat, double lng , double zoom){
        LngLat lngLat = new  LngLat(lng, lat);
        _mapController.animateCamera(new CameraPosition(_mapController , lngLat , (float) zoom, 0.0f , 0.0f) , 1000);
    }


    @Override
    public void nativeModuleCalled(int type , String bundle) {

        switch (type){
            case 1:
                LngLat lngLat = new LngLat(Double.parseDouble(bundle.split(";")[0]), Double.parseDouble(bundle.split(";")[1]));
                addMarker(lngLat);
                break;

            case 2:
            setCameraAnimate(Double.parseDouble(bundle.split(";")[1]) ,
                    Double.parseDouble(bundle.split(";")[0]), Double.parseDouble(bundle.split(";")[2]));
                break;
            default:
                break;

            case 3:
                lngLat = new LngLat(Double.parseDouble(bundle.split(";")[0]), Double.parseDouble(bundle.split(";")[1]));
                addCustomMarker(lngLat);
                break;
        }

    }
}
