package net.ddns.mahaljsp.googlemap;

import android.*;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TableLayout;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private static final int REQUEST_LOCATION_PERMISSION = 100;
    private GoogleMap mMap;
    Spinner spinner;
    Button btnControl;
    TableLayout menu_panel;
    private boolean showMenu;
    private UiSettings uiSettings;
    private Switch zoom_button_switch, compass_switch,
            traffic_switch, mylocation_button_switch,
            mylocation_layer_switch, scroll_gestures_switch,
            zoom_gestures_switch, tilt_gestures_switch,
            rotate_gestures_switch;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps2);
        processViews();
        processControllers();
        spinner.setSelection(1);
    }
    public void processViews(){
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        spinner=(Spinner)findViewById(R.id.spinner);
        btnControl=(Button)findViewById(R.id.btn_control);
        menu_panel=(TableLayout)findViewById(R.id.menu_panel);
        zoom_button_switch = (Switch) findViewById(R.id.zoom_button_switch);
        compass_switch = (Switch) findViewById(R.id.compass_switch);
        traffic_switch = (Switch) findViewById(R.id.traffic_switch);
        mylocation_button_switch = (Switch) findViewById(R.id.mylocation_button_switch);
        mylocation_layer_switch = (Switch) findViewById(R.id.mylocation_layer_switch);
        scroll_gestures_switch = (Switch) findViewById(R.id.scroll_gestures_switch);
        zoom_gestures_switch = (Switch) findViewById(R.id.zoom_gestures_switch);
        tilt_gestures_switch = (Switch) findViewById(R.id.tilt_gestures_switch);
        rotate_gestures_switch = (Switch) findViewById(R.id.rotate_gestures_switch);
    }
    public void processControllers(){
        spinner.setAdapter(ArrayAdapter.createFromResource(
                this,
                R.array.type_array,
                android.R.layout.simple_spinner_dropdown_item));
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                switch(position){
                    case 0:
                        mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
                        break;
                    case 1:
                        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                        break;
                    case 2:
                        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                        break;
                    case 3:
                        mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                        break;
                    case 4:
                        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                        break;
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {}
        });
        CompoundButton.OnCheckedChangeListener switchListener;
        switchListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView == zoom_button_switch) {
                    uiSettings.setZoomControlsEnabled(isChecked);
                }
                else if (buttonView == compass_switch) {
                    uiSettings.setCompassEnabled(isChecked);
                }
                else if (buttonView == traffic_switch) {
                    mMap.setTrafficEnabled(isChecked);
                }
                else if (buttonView == mylocation_button_switch) {
                    uiSettings.setMyLocationButtonEnabled(isChecked);
                }
                else if (buttonView == mylocation_layer_switch) {
                    processPermission();
                }
                else if (buttonView == scroll_gestures_switch) {
                    uiSettings.setScrollGesturesEnabled(isChecked);
                }
                else if (buttonView == zoom_gestures_switch) {
                    uiSettings.setZoomGesturesEnabled(isChecked);
                }
                else if (buttonView == tilt_gestures_switch) {
                    uiSettings.setTiltGesturesEnabled(isChecked);
                }
                else if (buttonView == rotate_gestures_switch) {
                    uiSettings.setRotateGesturesEnabled(isChecked);
                }
            }

        };
        zoom_button_switch.setOnCheckedChangeListener(switchListener);
        compass_switch.setOnCheckedChangeListener(switchListener);
        traffic_switch.setOnCheckedChangeListener(switchListener);
        mylocation_button_switch.setOnCheckedChangeListener(switchListener);
        mylocation_layer_switch.setOnCheckedChangeListener(switchListener);
        scroll_gestures_switch.setOnCheckedChangeListener(switchListener);
        zoom_gestures_switch.setOnCheckedChangeListener(switchListener);
        tilt_gestures_switch.setOnCheckedChangeListener(switchListener);
        rotate_gestures_switch.setOnCheckedChangeListener(switchListener);
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        uiSettings = mMap.getUiSettings();
    }
    public void btn_control_click(View view){
        showMenu=!showMenu;
        menu_panel.setVisibility(showMenu?View.VISIBLE:View.INVISIBLE);
    }
    private void processPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int hasPermission = checkSelfPermission(
                    android.Manifest.permission.ACCESS_FINE_LOCATION);
            if (hasPermission != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(
                        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                        REQUEST_LOCATION_PERMISSION);
                return;
            }
        }
        processMyLocation();
    }
    @Override
    public void onRequestPermissionsResult(
            int requestCode,
            String[] permissions,
            int[] grantResults) {
        if (requestCode == REQUEST_LOCATION_PERMISSION) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                processMyLocation();
            }
            else {
                Toast.makeText(this, "沒有讀取位置授權", Toast.LENGTH_SHORT).show();
            }
        }
        else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
    private void processMyLocation() throws SecurityException {
        mMap.setMyLocationEnabled(mylocation_layer_switch.isChecked());
    }

}
