package com.rn.s.baidumap.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.rn.s.baidumap.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sujialong on 2019/7/9.
 */

public class OverlayPolyline extends View implements OverlayView {

    private Polyline mPolyline;
    private List<LatLng> points;
    private List<BitmapDescriptor> textureList;
    private List<Integer> indexList;
    private int color = 0xAAFF0000;
    private int lineWidth;
    private boolean dottedLine;


    public OverlayPolyline(Context context) {
        super(context);
    }

    public int getLineWidth() {
        return lineWidth;
    }

    public void setLineWidth(int lineWidth) {
        this.lineWidth = lineWidth;
        if(mPolyline != null) {
            mPolyline.setWidth(lineWidth);
        }
    }

    public boolean getDottedLine() {
        return dottedLine;
    }

    public void setDottedLine(boolean dottedLine) {
        this.dottedLine = dottedLine;
        if(mPolyline != null) {
            mPolyline.setDottedLine(dottedLine);
        }
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
        if(mPolyline != null) {
            mPolyline.setColor(color);
        }
    }

    public List<LatLng> getPoints() {
        return points;
    }

    public List<BitmapDescriptor> getTextureList(){
        return textureList;
    }

    public List<Integer> getIndexList(){
        return indexList;
    }

    public void setPoints(List<LatLng> points, List<Integer> indexList, List<BitmapDescriptor> textureList) {
        this.points = points;
        this.indexList = indexList;
        this.textureList = textureList;
        if(mPolyline != null) {
            mPolyline.setPoints(points);
        }
    }

    @Override
    public Object getOverlayView() {
        return mPolyline;
    }

    @Override
    public void addTopMap(BaiduMap baiduMap) {
        //设置折线的属性
        // if(!getDottedLine()){
        //   OverlayOptions mOverlayOptions = new PolylineOptions()
        //         .width(getLineWidth())
        //         .color(getColor())
        //         .points(getPoints())
        //         .dottedLine(getDottedLine());
        //   mPolyline = (Polyline)baiduMap.addOverlay(mOverlayOptions);
        // }
        // else {
        //   OverlayOptions mOverlayOptions = new PolylineOptions()
        //           .width(getLineWidth())
        //           .points(getPoints())
        //           .customTextureList(getTextureList())
        //           .textureIndex(getIndexList())
        //           .dottedLine(getDottedLine());
        //   mPolyline = (Polyline)baiduMap.addOverlay(mOverlayOptions);
        // }

        OverlayOptions mOverlayOptions = new PolylineOptions()
                .width(getLineWidth())
                .color(getColor())
                .points(getPoints())
                .dottedLine(getDottedLine());
          mPolyline = (Polyline)baiduMap.addOverlay(mOverlayOptions);

    }

    @Override
    public void remove() {
        if(mPolyline != null) {
            mPolyline.remove();
            mPolyline = null;
        }
    }
}
