package com.tofly.ghrq_route.common.util.measure;

/**
 * 测量工具：长度、面积、角度的测量
 * Created by li on 2016/8/25.
 */

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

import androidx.appcompat.view.ActionMode;
import androidx.core.view.ActionProvider;
import androidx.core.view.MenuItemCompat;

import com.esri.arcgisruntime.geometry.AngularUnit;
import com.esri.arcgisruntime.geometry.AreaUnit;
import com.esri.arcgisruntime.geometry.GeodeticCurveType;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.LinearUnit;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PointCollection;
import com.esri.arcgisruntime.geometry.Polygon;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.Unit;
import com.esri.arcgisruntime.mapping.view.Callout;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.FillSymbol;
import com.esri.arcgisruntime.symbology.LineSymbol;
import com.esri.arcgisruntime.symbology.MarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.util.ListenableList;
import com.tofly.ghrq_route.common.util.degree.DegreeUtil;

import java.util.ArrayList;
import java.util.Iterator;

@SuppressWarnings("ResourceType")
public class MeasuringTool implements ActionMode.Callback {
    private static final long serialVersionUID = 1L;
    private static final int MENU_DELETE = 0;
    private static final int MENU_PREF = 1;
    private static final int MENU_UNDO = 2;
    private MapView mMap;
    private GraphicsOverlay mLayer;//测量需要绘制的图形的底图，依附在mapView上
    private View.OnTouchListener mOldOnSingleTapListener;
    private MarkerSymbol mMarkerSymbol;//点的样式
    private LineSymbol mLineSymbol;//线的样式
    private double mResult; //计算结果
    private TextView mText;//弹出框的tv
    private MeasuringTool.MeasureType mMeasureMode;//测量的类型：长度，面积，角度
    private int mCurrentLinearUnit;//使用的长度单位
    private Unit[] mLinearUnits;
    private Unit[] mDefaultLinearUnits;
    private int mCurrentAreaUnit;//使用的面积单位
    private Unit[] mAreaUnits;//新设置的面积单位数组
    private Unit[] mDefaultAreaUnits;//默认的面积单位数组
    private int mCurrentAngularUnit;//使用的角度单位
    private Unit[] mAngularUnit;//新设置的角度单位数组
    private Unit[] mDefaultAngularUnit;//默认的角度单位数组
    private Context mContext;
    private ArrayList<Point> mPoints;
    private FillSymbol mFillSymbol;//面积样式
    private Callout mCallout;
    private ActionMode mMode;//
    private Polyline mLine;//多线
    private Polygon mPolygon;//多边形

    //构造函数中设置默认的点线面样式，以及默认的测量单位
    public MeasuringTool(MapView map) {
        this.mMeasureMode = MeasuringTool.MeasureType.LINEAR;
        this.mMap = map;
        this.mContext = this.mMap.getContext();
        this.mMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, -65536, 10);
        this.mLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, -16777216, 3.0F);
        this.mDefaultLinearUnits = new Unit[] {Unit.fromUnitId(9001), Unit.fromUnitId(9036), Unit.fromUnitId(9002), Unit.fromUnitId(9093)};
        this.mDefaultAreaUnits = new Unit[]{Unit.fromUnitId(109404), Unit.fromUnitId(109414), Unit.fromUnitId(109405), Unit.fromUnitId(109439)};
        this.mDefaultAngularUnit = new Unit[]{Unit.fromUnitId(9102), Unit.fromUnitId(9101)};
        this.mFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID,
                Color.argb(100, 225, 225, 0), null);
        this.mFillSymbol.setOutline(new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID ,0, 0.0F));
    }

    private void init() {
        this.mOldOnSingleTapListener = this.mMap.getOnTouchListener();
        this.mMap.setOnTouchListener(new DefaultMapViewOnTouchListener(mContext, mMap){
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                onSingleTap(e.getX(), e.getY());
                return true;
            }
        });
        this.mLayer = new GraphicsOverlay();
        this.mMap.getGraphicsOverlays().add(this.mLayer);
        this.mPoints = new ArrayList<>();
    }

    public GraphicsOverlay getGraphicOverlay() {
        return mLayer;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                this.deleteAll();
                break;
            case 2:
                this.undo();
        }

        return false;
    }

    //删除全部点
    public void deleteAll() {
        this.mLayer.getGraphics().clear();
        this.mResult = 0.0D;
        this.mPoints.clear();
        this.showResult();
        this.updateMenu();
    }

    //添加点击测量后ActionBar上的操作按钮
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        this.mMode = mode;
        this.init();
        MenuItem item = menu.add(0, 2, 1, "返回");//返回上一次操作
        item.setIcon(android.R.drawable.ic_menu_revert);
        item.setVisible(false);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        item = menu.add(0, 0, 2, "清理全部");//清除全部
        item.setIcon(android.R.drawable.ic_menu_delete);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        item.setVisible(false);
        item = menu.add(0, 1, 3, "参数选择");
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        item.setIcon(android.R.drawable.ic_menu_manage);
        //item.setActionProvider(new MeasuringTool.Preferences(this.mContext));
        MenuItemCompat.setActionProvider(item, new Preferences(this.mContext));
        return true;
    }

    //当ActionBar上面不显示测量的工具图标时调用
    public void onDestroyActionMode(ActionMode mode) {
        this.hideCallout();
        this.mMap.getGraphicsOverlays().remove(this.mLayer);
        this.mLayer = null;
        this.mMap.setOnTouchListener(this.mOldOnSingleTapListener);
        this.mPoints = null;
    }

    //隐藏弹出框
    private void hideCallout() {
        if (this.mCallout != null && this.mCallout.isShowing()) {
            this.mCallout.dismiss();
        }

    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

    //单击就添加点到集合中
    public void onSingleTap(float x, float y) {
        this.addPoint(x, y);
    }

    //在地图上添加点击的点，并按不同的模式显示不同的内容
    private void addPoint(float x, float y) {
        android.graphics.Point screenPoint =
                new android.graphics.Point(Math.round(x), Math.round(y));
        Point point = this.mMap.screenToLocation(screenPoint);
        this.mPoints.add(point);
        this.clearAndDraw();
    }

    //撤销
    private void undo() {
        this.mPoints.remove(this.mPoints.size() - 1);
        this.clearAndDraw();
    }

    //清除当前的点并重新绘制点和计算
    private void clearAndDraw() {
        ListenableList oldGraphics = this.mLayer.getGraphics();
        this.mLayer.getGraphics().removeAll(oldGraphics);
        this.draw();
        this.updateMenu();
    }

    /**
     * 在地图上描点
     */
    private void draw() {
        if (this.mPoints.size() != 0) {
            int index = 0;
            this.mResult = 0.0D;

            //this.mPolygon = new Polygon();//多边形
            PointCollection collection = new PointCollection(mMap.getSpatialReference());
            Point screenPoint;
            for (Iterator labelPointForPolygon = this.mPoints.iterator(); labelPointForPolygon.hasNext(); ++index) {
                screenPoint = (Point) labelPointForPolygon.next();
                this.mLayer.getGraphics().add(new Graphic(screenPoint, this.mMarkerSymbol));
                if (index == 0) {
                    collection.add(screenPoint);
                    //this.mLine.startPath(screenPoint);//多线起始点
                    if (this.mMeasureMode == MeasuringTool.MeasureType.AREA) {
                        //this.mPolygon.startPath(screenPoint);//如果类型为面积，绘制多边形起始点
                    }
                } else {
                    collection.add(screenPoint);
                    //this.mLine.lineTo(screenPoint);//绘制到下个点
                    if (this.mMeasureMode == MeasuringTool.MeasureType.AREA) {
                        //this.mPolygon.lineTo(screenPoint);
                    }
                }
                //添加到图层中
                this.mLine = new Polyline(collection);//多线
                this.mLayer.getGraphics().add(new Graphic(this.mLine, this.mLineSymbol));
            }

            Point var4;//显示的弹窗的点
            if (this.mMeasureMode == MeasuringTool.MeasureType.LINEAR) {//计算长度的模式
                //计算长度
                this.mResult += GeometryEngine.lengthGeodetic(this.mLine
                        , (LinearUnit) this.getLinearUnit(this.mCurrentLinearUnit)
                        , GeodeticCurveType.GEODESIC);
                android.graphics.Point screen = this.mMap.locationToScreen(this.mPoints.get(index - 1));
                var4 = new Point(screen.x, screen.y);
                this.showResult((float) var4.getX(), (float) var4.getY());// TODO: 2016/8/26 修改是否隐藏callout，分段显示长度
            } else if (this.mMeasureMode == MeasuringTool.MeasureType.AREA) {//计算面积的模式
                collection.add(this.mPoints.get(0));
                this.mLine = new Polyline(collection);//多线
                //this.mLine.lineTo((Point) this.mPoints.get(0));//绘制到起始点
                this.mLayer.getGraphics().add(new Graphic(this.mLine, this.mLineSymbol));
                this.mPolygon = new Polygon(collection);
                //this.mPolygon.lineTo((Point) this.mPoints.get(0));//封闭多边形
                this.mLayer.getGraphics().add(new Graphic(this.mPolygon, this.mFillSymbol));
                //计算面积
                this.mResult = GeometryEngine.areaGeodetic(this.mPolygon
                        , (AreaUnit) this.getAreaUnit(this.mCurrentAreaUnit),
                        GeodeticCurveType.GEODESIC);
                var4 = GeometryEngine.labelPoint(this.mPolygon);
                /*var4 = GeometryEngine.getLabelPointForPolygon(this.mPolygon
                        , this.mMap.getSpatialReference());*/
                android.graphics.Point screen = this.mMap.locationToScreen(var4);
                this.showResult((float) screen.x, (float) screen.y);
            } else if (this.mMeasureMode == MeasureType.ANGULAR) {//计算角度的模式
                int i = index - 1;
                //当点击的点大于等于3时，才能计算角度
                if (i >= 2) {
                    this.mResult = DegreeUtil.MeasuringDegree(mPoints.get(i - 2)
                            , mPoints.get(i - 1)
                            , mPoints.get(i)
                            , (AngularUnit) this.getAngularUnite(this.mCurrentAngularUnit));
                    //显示在第二个点上
                    android.graphics.Point screen = this.mMap.locationToScreen(this.mPoints.get(index - 2));
                    var4 = new Point(screen.x, screen.y);
                    this.showResult((float) var4.getX(), (float) var4.getY());
                } else {
                    hideCallout();
                }
            }
        }
    }

    //刷新“撤销”键和“清除全部”键
    private void updateMenu() {
        this.mMode.getMenu().findItem(0).setVisible(this.mPoints.size() > 0);
        this.mMode.getMenu().findItem(2).setVisible(this.mPoints.size() > 0);
    }

    /**
     * 显示结果到地图上
     *
     * @param x ，y  callout的坐标
     */
    private void showResult(float x, float y) {
        if (this.mResult > 0.0D) {
            if (this.mCallout == null) {
                this.mText = new TextView(this.mContext);
                this.mCallout = this.mMap.getCallout();
            }
            this.mText.setText(this.getResultString());
            android.graphics.Point screenPoint =
                    new android.graphics.Point(Math.round(x), Math.round(y));
            Point mapPoint = this.mMap.screenToLocation(screenPoint);
            this.mCallout.show(mText, mapPoint);
        } else if (this.mCallout != null && this.mCallout.isShowing()) {
            this.mCallout.dismiss();
        }
    }

    //当已经显示了弹出框，刷新弹出框
    private void showResult() {
        if (this.mResult > 0.0D) {
            this.mText.setText(this.getResultString());
            this.mCallout.show();
        } else if (this.mCallout != null && this.mCallout.isShowing()) {
            this.mCallout.dismiss();
        }

    }

    /**
     * 设置测量长度的工具
     *
     * @param linearUnits 工具单位数组
     */
    public void setLinearUnits(Unit[] linearUnits) {
        this.mLinearUnits = linearUnits;
    }

    //获取测量长度的单位
    Unit getLinearUnit(int position) {
        return this.mLinearUnits == null ?
                this.mDefaultLinearUnits[position % mDefaultLinearUnits.length] :
                this.mLinearUnits[position % mLinearUnits.length];
    }

    //获取长度单位的数组长度
    int getLinearUnitSize() {
        return this.mLinearUnits == null ? this.mDefaultLinearUnits.length : this.mLinearUnits.length;
    }

    /**
     * 设置测量面积的单位集合
     *
     * @param areaUnits 面积单位数组
     */
    public void setAreaUnits(Unit[] areaUnits) {
        this.mAreaUnits = areaUnits;
    }


    //获取计算面积的单位
    private Unit getAreaUnit(int position) {
        return this.mAreaUnits == null ?
                this.mDefaultAreaUnits[position % mDefaultAreaUnits.length] :
                this.mAreaUnits[position % mAreaUnits.length];
    }


    //获取测量的面积的单位的数组长度
    private int getAreaUnitSize() {
        return this.mAreaUnits == null ? this.mDefaultAreaUnits.length : this.mAreaUnits.length;
    }

    /**
     * 设置测量角度的单位集合
     *
     * @param angularUnits 角度单位数组
     */
    private void setAngularUnits(Unit[] angularUnits) {
        this.mAngularUnit = angularUnits;
    }

    //获取计算角度的单位
    private Unit getAngularUnite(int position) {
        return this.mAngularUnit == null ?
                this.mDefaultAngularUnit[position % mDefaultAngularUnit.length] :
                this.mAngularUnit[position % mAngularUnit.length];
    }

    //获取测量角度的单位的数组长度
    private int getAngularUnitSize() {
        return this.mAngularUnit == null ? this.mDefaultAngularUnit.length : this.mAngularUnit.length;
    }


    //根据当前的模式获取不同单位的数组长度
    private int getUnitSize() {
        return this.mMeasureMode == MeasuringTool.MeasureType.LINEAR ?
                this.getLinearUnitSize() : this.mMeasureMode == MeasureType.AREA ?
                this.getAreaUnitSize() : this.getAngularUnitSize();
    }

    //根据当前的模式选择单位
    private Unit getUnit(int position) {
        return this.mMeasureMode == MeasuringTool.MeasureType.LINEAR ?
                this.getLinearUnit(position) : this.mMeasureMode == MeasureType.AREA ?
                this.getAreaUnit(position) : this.getAngularUnite(position);
    }

    //根据当前的模式获取不同的使用单位
    private Unit getCurrentUnit() {
        return this.getUnit(this.mMeasureMode == MeasuringTool.MeasureType.LINEAR ?
                this.mCurrentLinearUnit : this.mMeasureMode == MeasureType.AREA ?
                this.mCurrentAreaUnit : this.mCurrentAngularUnit);
    }

    private String getUnitDisplayNameCn(Unit unit){
        String nameCn = unit.getDisplayName();
        switch (unit.getUnitId()){
            case 9001:
                nameCn = "米";
                break;
            case 9036:
                nameCn = "千米";
                break;
            case 9002:
                nameCn = "英尺";
                break;
            case 9093:
                nameCn = "英里";
                break;
            case 109404:
                nameCn = "平方米";
                break;
            case 109414:
                nameCn = "平方千米";
                break;
            case 109405:
                nameCn = "平方英尺";
                break;
            case 109439:
                nameCn = "平方英里";
                break;
            case 9102:
                nameCn = "度";
                break;
            case 9101:
                nameCn = "弧度";
                break;

        }
        return  nameCn;
    }

    /**
     * 设置绘制的线的样式
     *
     * @param symbol 线样式
     */
    public void setLineSymbol(LineSymbol symbol) {
        this.mLineSymbol = symbol;
    }

    /**
     * 设置标记点的样式
     *
     * @param symbol 标记点样式
     */
    public void setMarkerSymbol(MarkerSymbol symbol) {
        this.mMarkerSymbol = symbol;
    }

    /**
     * 设置面积图形的样式
     *
     * @param symbol 样式
     */
    public void setFillSymbol(FillSymbol symbol) {
        this.mFillSymbol = symbol;
    }

    private Geometry getGeometry() {
        return (this.mMeasureMode == MeasureType.AREA ? this.mPolygon : this.mLine);
    }

    @SuppressLint("DefaultLocale")
    private String getResultString() {
        return this.mResult > 0.0D ?
                String.format("%.2f", new Object[]{Double.valueOf(this.mResult)})
                        + " "
                        + getUnitDisplayNameCn(this.getCurrentUnit())
                : "";
    }

    /**
     * 设置单位选项的ActionProvider
     */
    class Preferences extends ActionProvider {
        private ImageView imageView;

        public Preferences(Context context) {
            super(context);
            this.imageView = new ImageView(MeasuringTool.this.mContext);
            this.imageView.setImageDrawable(context.getResources().getDrawable(
                    android.R.drawable.ic_menu_manage));
        }

        public View onCreateActionView() {
            Spinner spinner = new Spinner(MeasuringTool.this.mContext);//下拉按钮
            spinner.setAdapter(new BaseAdapter() {

                public View getView(int position, View convertView, ViewGroup parent) {
                    return Preferences.this.imageView;
                }

                public View getDropDownView(int position, View convertView, ViewGroup parent) {
                    LinearLayout layout;
                    TextView text;
                    //选择测量类型
                    if (position == 0) {
                        layout = new LinearLayout(MeasuringTool.this.mContext);
                        layout.setOrientation(LinearLayout.VERTICAL);
                        text = new TextView(MeasuringTool.this.mContext);
                        text.setText("选择测量类型");
                        text.setTextColor(MeasuringTool.this.mContext.getResources().getColor(android.R.color.holo_blue_light));
                        text.setTextSize(16.0F);
                        layout.addView(text);
                        RadioButton varLen = new RadioButton(MeasuringTool.this.mContext);
                        varLen.setText("长度");
                        RadioButton varArea = new RadioButton(MeasuringTool.this.mContext);
                        varArea.setText("面积");
                        RadioButton varAngu = new RadioButton(MeasuringTool.this.mContext);
                        varAngu.setText("角度");
                        RadioGroup geoType = new RadioGroup(MeasuringTool.this.mContext);
                        geoType.addView(varLen);
                        geoType.addView(varArea);
                        geoType.addView(varAngu);
                        //记录当前选中的测量模式
                        geoType.check(MeasuringTool.this.mMeasureMode == MeasureType.LINEAR ?
                                varLen.getId() : MeasuringTool.this.mMeasureMode == MeasureType.AREA ?
                                varArea.getId() : varAngu.getId());
                        layout.addView(geoType);
                        layout.setPadding(10, 10, 10, 10);
                        geoType.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                            public void onCheckedChanged(RadioGroup rGroup, int checkedId) {
                                for (int i = 0; i < rGroup.getChildCount(); ++i) {
                                    if (rGroup.getChildAt(i).getId() == checkedId) {
                                        MeasuringTool.this.mMeasureMode = MeasuringTool.MeasureType.getType(i);
                                        notifyDataSetChanged();
                                        MeasuringTool.this.clearAndDraw();
                                    }
                                }

                            }
                        });
                        return layout;
                    } else {//选择单位
                        layout = new LinearLayout(MeasuringTool.this.mContext);
                        layout.setOrientation(LinearLayout.VERTICAL);
                        text = new TextView(MeasuringTool.this.mContext);
                        text.setText("选择单位");
                        text.setTextColor(MeasuringTool.this.mContext.getResources().getColor(android.R.color.holo_blue_light));
                        text.setTextSize(16.0F);
                        layout.addView(text);
                        RadioGroup group = new RadioGroup(MeasuringTool.this.mContext);
                        for (int i = 0; i < MeasuringTool.this.getUnitSize(); ++i) {
                            RadioButton r = new RadioButton(MeasuringTool.this.mContext);
                            r.setText(getUnitDisplayNameCn(MeasuringTool.this.getUnit(i)));
                            group.addView(r);
                            //设置并保存选中的单位
                            if (i == (MeasuringTool.this.mMeasureMode == MeasuringTool.MeasureType.LINEAR ?
                                    MeasuringTool.this.mCurrentLinearUnit : MeasuringTool.this.mMeasureMode == MeasureType.AREA ?
                                    MeasuringTool.this.mCurrentAreaUnit : MeasuringTool.this.mCurrentAngularUnit)) {
                                group.check(r.getId());
                            }
                        }
                        //通过选择的单位，设置系统的单位
                        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                            public void onCheckedChanged(RadioGroup rGroup, int checkedId) {
                                for (int i = 0; i < rGroup.getChildCount(); ++i) {
                                    if (rGroup.getChildAt(i).getId() == checkedId) {
                                        if (MeasuringTool.this.mMeasureMode == MeasuringTool.MeasureType.LINEAR) {
                                            if (MeasuringTool.this.mResult > 0.0D) {
                                                LinearUnit lineUnit = (LinearUnit) MeasuringTool.this.getLinearUnit(MeasuringTool.this.mCurrentLinearUnit);
                                                MeasuringTool.this.mResult = lineUnit.convertTo((LinearUnit) MeasuringTool.this.getLinearUnit(i), MeasuringTool.this.mResult);
                                                //MeasuringTool.this.mResult = Unit.convertUnits(MeasuringTool.this.mResult, MeasuringTool.this.getLinearUnit(MeasuringTool.this.mCurrentLinearUnit), MeasuringTool.this.getLinearUnit(i));
                                                MeasuringTool.this.mCurrentLinearUnit = i;
                                                MeasuringTool.this.showResult();
                                            } else {
                                                MeasuringTool.this.mCurrentLinearUnit = i;
                                            }
                                        } else if (MeasuringTool.this.mMeasureMode == MeasureType.AREA) {
                                            if (MeasuringTool.this.mResult > 0.0D) {
                                                AreaUnit areaUnit = (AreaUnit) MeasuringTool.this.getAreaUnit(MeasuringTool.this.mCurrentAreaUnit);
                                                MeasuringTool.this.mResult = areaUnit.convertTo((AreaUnit) MeasuringTool.this.getAreaUnit(i), MeasuringTool.this.mResult);
                                                //MeasuringTool.this.mResult = Unit.convertUnits(MeasuringTool.this.mResult, MeasuringTool.this.getAreaUnit(MeasuringTool.this.mCurrentAreaUnit), MeasuringTool.this.getAreaUnit(i));
                                                MeasuringTool.this.mCurrentAreaUnit = i;
                                                MeasuringTool.this.showResult();
                                            } else {
                                                MeasuringTool.this.mCurrentAreaUnit = i;
                                            }
                                        } else if (MeasuringTool.this.mMeasureMode == MeasureType.ANGULAR) {
                                            if (MeasuringTool.this.mResult > 0.0D) {
                                                AngularUnit angularUnit = (AngularUnit) MeasuringTool.this.getAngularUnite(MeasuringTool.this.mCurrentAngularUnit);
                                                MeasuringTool.this.mResult = angularUnit.convertTo((AngularUnit) MeasuringTool.this.getAngularUnite(i), MeasuringTool.this.mResult);
                                                //MeasuringTool.this.mResult = Unit.convertUnits(MeasuringTool.this.mResult, MeasuringTool.this.getAngularUnite(MeasuringTool.this.mCurrentAngularUnit), MeasuringTool.this.getAngularUnite(i));
                                                MeasuringTool.this.mCurrentAngularUnit = i;
                                                MeasuringTool.this.showResult();
                                            } else {
                                                MeasuringTool.this.mCurrentAngularUnit = i;
                                            }
                                        }
                                    }
                                }

                            }
                        });
                        layout.addView(group);
                        layout.setPadding(10, 10, 10, 10);
                        return layout;
                    }
                }

                public long getItemId(int position) {
                    return (long) position;
                }

                public Object getItem(int position) {
                    return null;
                }

                public int getCount() {
                    return 2;
                }
            });
            return spinner;
        }
    }

    public enum MeasureType {
        LINEAR,//长度模式
        AREA,//面积模式
        ANGULAR;//角度模式

        /**
         * 提供静态方法，供外部获取枚举类型
         *
         * @param i 0表示LINEAR长度 1表示AREA面积 2表示角度
         */
        public static MeasuringTool.MeasureType getType(int i) {
            switch (i) {
                case 0:
                    return LINEAR;
                case 1:
                    return AREA;
                case 2:
                    return ANGULAR;
                default:
                    return LINEAR;
            }
        }
    }
}
