package com.example.geo_api16;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;


import java.util.Random;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;

//public class Sample extends AppCompatActivity implements OnMapReadyCallback, OnCameraChangeListener, OnCameraIdleListener  
//{

public class Sample extends AppCompatActivity implements OnMapReadyCallback, OnCameraChangeListener   {

    private GoogleMap map;
    private MapScaleView scaleView;
    private MapScaleView scaleViewMiles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scale_layout);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment);
        mapFragment.getMapAsync(this);

    //    scaleView = (MapScaleView) findViewById(R.id.scaleView);
     //   scaleViewMiles = (MapScaleView) findViewById(R.id.scaleViewMiles);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;

        googleMap.setOnCameraChangeListener(this);
        googleMap.setOnCameraChangeListener(this);
        googleMap.setOnCameraChangeListener(this);

//        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(21, 105.8), 10));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.07770360532252, -94.76820822805165), 12));
    }

    public void onCameraMove() {
        update(map.getCameraPosition());
    }

    public void onCameraIdle() {
        update(map.getCameraPosition());
    }

    @Override
    public void onCameraChange(CameraPosition cameraPosition) {
        Log.d("++++", "pos " + cameraPosition);
        update(cameraPosition);
    }

    private void update(CameraPosition cameraPosition) {
        scaleView.update(cameraPosition.zoom, cameraPosition.target.latitude);
        scaleViewMiles.update(cameraPosition.zoom, cameraPosition.target.latitude);
    }

    public void changeColor(View view) {
        Random random = new Random();
        int color = Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255));
        scaleView.setColor(color);
    }

    public void changeTextSize(View view) {
        scaleView.setTextSize(new Random().nextInt(20) + 18);
    }

    public void changeStrokeWidth(View view) {
        scaleView.setStrokeWidth(new Random().nextInt(7) + 1);
    }

    private boolean isMiles = false;

    public void changeMiles(View view) {
        isMiles = !isMiles;
        scaleView.setIsMiles(isMiles);
    }

    public void changeSize(View view) {
        ViewGroup.LayoutParams layoutParams = scaleView.getLayoutParams();
        layoutParams.width = new Random().nextInt(200) + 200;
        scaleView.requestLayout();
    }
} 
