/* Copyright 2016 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.esri.arcgisruntime.sample.uniquevaluerenderer;

import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.esri.arcgisruntime.datasource.arcgis.ServiceFeatureTable;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.UniqueValueRenderer;


public class MainActivity extends AppCompatActivity {

    private MapView mMapView;

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

        // inflate MapView from layout
        mMapView = (MapView) findViewById(R.id.mapView);

        // create a map with the topographic basemap
        ArcGISMap map = new ArcGISMap(Basemap.createTopographic());

        //[DocRef: Name=Unique Value Renderer, Topic=Symbols and Renderers, Category=Fundamentals]
        // Create service feature table
        ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(getResources().getString(R.string.sample_service_url));

        // Create the feature layer using the service feature table
        FeatureLayer featureLayer = new FeatureLayer(serviceFeatureTable);

        // Override the renderer of the feature layer with a new unique value renderer
        UniqueValueRenderer uniqueValueRenderer = new UniqueValueRenderer();
        // Set the field to use for the unique values
        uniqueValueRenderer.getFieldNames().add("STATE_ABBR"); //You can add multiple fields to be used for the renderer in the form of a list, in this case we are only adding a single field

        // Create the symbols to be used in the renderer
        SimpleFillSymbol defaultFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.NULL, Color.BLACK, new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.GRAY, 2));
        SimpleFillSymbol californiaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.RED, new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 2));
        SimpleFillSymbol arizonaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.GREEN, new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.GREEN, 2));
        SimpleFillSymbol nevadaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID,Color.BLUE, new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 2));

        // Set default symbol
        uniqueValueRenderer.setDefaultSymbol(defaultFillSymbol);
        uniqueValueRenderer.setDefaultLabel("Other");

        // Set value for california
        List californiaValue = new ArrayList();
        californiaValue.add("CA"); //You add values associated with fields set on the unique value renderer. If there are multiple values, they should be set in the same order as the fields are set
        uniqueValueRenderer.getUniqueValues().add(new UniqueValueRenderer.UniqueValue("California", "State of California", californiaFillSymbol, californiaValue));

        // Set value for arizona
        List arizonaValue = new ArrayList();
        arizonaValue.add("AZ"); //You add values associated with fields set on the unique value renderer. If there are multiple values, they should be set in the same order as the fields are set
        uniqueValueRenderer.getUniqueValues().add(new UniqueValueRenderer.UniqueValue("Arizona", "State of Arizona", arizonaFillSymbol, arizonaValue));

        // Set value for nevada
        List nevadaValue = new ArrayList();
        nevadaValue.add("NV"); //You add values associated with fields set on the unique value renderer. If there are multiple values, they should be set in the same order as the fields are set
        uniqueValueRenderer.getUniqueValues().add(new UniqueValueRenderer.UniqueValue("Nevada", "State of Nevada", nevadaFillSymbol, nevadaValue));

        // Set the renderer on the feature layer
        featureLayer.setRenderer(uniqueValueRenderer);
        //[DocRef: END]

        // add the layer to the map
        map.getOperationalLayers().add(featureLayer);

        map.setInitialViewpoint(new Viewpoint(new Envelope(-13893029.0, 3573174.0, -12038972.0, 5309823.0, SpatialReferences.getWebMercator())));

        // set the map to be displayed in the mapview
        mMapView.setMap(map);

    }

    @Override
    protected void onPause(){
        super.onPause();
        // pause MapView
        mMapView.pause();
    }


    @Override
    protected void onResume(){
        super.onResume();
        // resume MapView
        mMapView.resume();
    }
}

@@ -3656,16 +3656,24 @@
     List
+%3CObject%3E
  califor
@@ -3688,32 +3688,34 @@
  = new ArrayList
+%3C%3E
 ();%0A        cali
@@ -3713,38 +3713,12 @@
     
- californiaValue.add(%22CA%22);
  //
+ 
 You 
@@ -3776,32 +3776,43 @@
  value renderer.
+%0A        //
  If there are mu
@@ -3877,32 +3877,67 @@
  fields are set%0A
+        californiaValue.add(%22CA%22);%0A
         uniqueVa
@@ -4130,16 +4130,24 @@
     List
+%3CObject%3E
  arizona
@@ -4159,32 +4159,34 @@
  = new ArrayList
+%3C%3E
 ();%0A        ariz
@@ -4184,35 +4184,12 @@
     
- arizonaValue.add(%22AZ%22);
  //
+ 
 You 
@@ -4247,32 +4247,43 @@
  value renderer.
+%0A        //
  If there are mu
@@ -4348,32 +4348,64 @@
  fields are set%0A
+        arizonaValue.add(%22AZ%22);%0A
         uniqueVa
@@ -4585,16 +4585,24 @@
     List
+%3CObject%3E
  nevadaV
@@ -4621,16 +4621,18 @@
 rrayList
+%3C%3E
 ();%0A    
@@ -4638,34 +4638,12 @@
     
- nevadaValue.add(%22NV%22);
  //
+ 
 You 
@@ -4709,16 +4709,27 @@
 enderer.
+%0A        //
  If ther
@@ -4802,32 +4802,63 @@
  fields are set%0A
+        nevadaValue.add(%22NV%22);%0A
         uniqueVa

refactor unchecked call