package com.oo.eye.widget;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.View;

import com.oo.eye.bean.ChartsBean;
import com.oo.eye.bean.EyeChartsBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xucon on 2017/12/28.
 */

public class ChartsView extends View {
	private List<ChartsBean> mChartsBeen;
	private int height;
	private int width;
	private int yTextWidth = 100;
	private float xSpace;
	private float ySpace;
	private float XTextPintSize = 40f;
	private int radius = 30;

	private Paint mGesturePaint = new Paint();
	private Paint circlePaint = new Paint();
	private Paint xTextPaint = new Paint();
	private Paint linePaint = new Paint();
	public static final float TOP_DP = 20f; // 坐标系距离顶部的间距
	public static final float BOTTOM_DP = 20f; // 坐标系距离底部的间距
	public static final float LEFT_DP = 20f; // 坐标系距离左边的间距
	public static final float RIGHT_DP = 20f; // 坐标系距离右边的间距

	public ChartsView(Context context, int width, int height){
		super(context);
		this.width = width;
		this.height = height;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		if(mChartsBeen != null && mChartsBeen.size() > 0) {

			xTextPaint.setColor(Color.WHITE);
			xTextPaint.setAntiAlias(true);
			xTextPaint.setStrokeWidth(1);
			xTextPaint.setTextSize(XTextPintSize);

			linePaint.setColor(Color.WHITE);
			linePaint.setAntiAlias(true);
			linePaint.setStrokeWidth(3);

			circlePaint.setColor(Color.parseColor("#FFF67F"));
			circlePaint.setAntiAlias(true);
			circlePaint.setStrokeWidth(3);

			mGesturePaint.setAntiAlias(true);
			mGesturePaint.setStyle(Paint.Style.STROKE);
			mGesturePaint.setStrokeWidth(5);
			mGesturePaint.setColor(Color.parseColor("#FFF67F"));

			/* 绘制x,y */
			canvas.drawLine(LEFT_DP+yTextWidth, height - BOTTOM_DP - XTextPintSize ,width- RIGHT_DP,height - BOTTOM_DP - XTextPintSize,linePaint);
			canvas.drawLine(LEFT_DP+yTextWidth, TOP_DP ,LEFT_DP+yTextWidth,height - BOTTOM_DP - XTextPintSize,linePaint);

			Path path = null;

			for (int i = mChartsBeen.size()-1; i >= 0; i--) {
				ChartsBean energy = mChartsBeen.get(i);
				if(path == null){
					path = new Path();
					path.moveTo(energy.x, energy.y);    //原点
				}else{
					path.lineTo(energy.x, energy.y);
				}

				float tSize = xTextPaint.measureText(energy.xText);
				canvas.drawCircle(energy.x, energy.y, radius/2, circlePaint);
				// 绘制y的 文字
				canvas.drawText(energy.yText, LEFT_DP,energy.y , xTextPaint);
				// 绘制x的 文字
				canvas.drawText(energy.xText, energy.x - tSize/2,height - BOTTOM_DP , xTextPaint);
				//画横线
				canvas.drawLine(LEFT_DP+yTextWidth, energy.y, width-RIGHT_DP, energy.y, linePaint);
				//画竖线
				canvas.drawLine(energy.x, TOP_DP, energy.x, height - BOTTOM_DP - XTextPintSize, linePaint);
			}
			if( mChartsBeen.size() > 1) {
				canvas.drawPath(path, mGesturePaint);
			}
		}
	}

	public void setData(List<EyeChartsBean> data, boolean isLeft){
		if(data == null || data.size() == 0)return;
		List<EyeChartsBean> beans = new ArrayList<>();
		for(int i = data.size()-1; i>= 0;i--){
			if(data.get(i) != null && data.get(i).getCreated_month() != null){
				beans.add(data.get(i));
			}
		}
		if(beans == null || beans.size() == 0)return;
		ArrayList<Double> values = new ArrayList<>();
		Map<Double,Float> map = new HashMap<>();
		ArrayList<String> vs = new ArrayList<>();
		int count = 0;
		for(EyeChartsBean bean : beans ){
			if(isLeft){
				if(!vs.contains(String.valueOf(bean.getLeft_eye()))){
					count++;
					values.add(bean.getLeft_eye());
					vs.add(String.valueOf(bean.getLeft_eye()));
				}
			}else{
				if(!vs.contains(String.valueOf(bean.getRight_eye()))){
					count++;
					values.add(bean.getRight_eye());
					vs.add(String.valueOf(bean.getRight_eye()));
				}
			}
		}
		xSpace = (width - LEFT_DP - yTextWidth - RIGHT_DP)/(beans.size()+ 1);
		ySpace = (height - TOP_DP - BOTTOM_DP - XTextPintSize)/(count+1);
		float h = height - BOTTOM_DP - XTextPintSize;
		Collections.sort(values);
		for(int i  = 0 ; i < values.size() ; i ++){
			map.put(values.get(i),h - (i+1)*ySpace);
		}
		mChartsBeen = new ArrayList<>();
		for(int j = 0; j < beans.size(); j++){
			try {
				EyeChartsBean bean = beans.get(j);
				ChartsBean chartsBean = new ChartsBean();
				chartsBean.x = LEFT_DP + yTextWidth + (j + 1) * xSpace;
				if (isLeft) {
					chartsBean.y = map.get(bean.getLeft_eye());
					chartsBean.yText = String.valueOf(bean.getLeft_eye());
				} else {
					chartsBean.y = map.get(bean.getRight_eye());
					chartsBean.yText = String.valueOf(bean.getRight_eye());
				}

				chartsBean.xText = bean.getCreated_month().substring(
						bean.getCreated_month().indexOf("-") + 1, bean.getCreated_month().length()) + "月";
				mChartsBeen.add(chartsBean);
			}catch (Exception e){
				e.printStackTrace();
			}
		}
		invalidate();
	}
}
