package com.yuqirong.waveview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.yuqirong.waveview.R;

/**
 * Created by Administrator on 2016/1/29.
 */
public class WaveView extends View {

    // 波浪颜色
    private int waveColor;

    private float waterHeight;

    private int secondWaveColor;
    // 默认波浪颜色
    private int defaultWaveColor;

    private int defaultSecondWaveColor;
    // 波浪高度
    private float waveHeight;
    // 默认波浪高度
    private float defaultWaveHeight;

    private Paint mPaint;

    private int mWidth;

    private int mHeight;

    private float w;

    private float speed = 7;

    private long startTime;

    private static final String TAG = "WaveView";

    public WaveView(Context context) {
        this(context, null);
    }

    public WaveView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.WaveView);
        defaultWaveColor = getResources().getColor(R.color.indigo_color);
        waveColor = a.getColor(R.styleable.WaveView_waveColor, defaultWaveColor);
        defaultSecondWaveColor = getResources().getColor(R.color.second_indigo_color);
        secondWaveColor = a.getColor(R.styleable.WaveView_secondWaveColor, defaultSecondWaveColor);
        defaultWaveHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
        waveHeight = a.getDimension(R.styleable.WaveView_waveHeight, defaultWaveHeight);
        a.recycle();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(waveColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = widthSize;
        } else {
            mWidth = 200;
        }

        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = heightSize;
        } else {
            mHeight = 200;
        }
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.w = (float) (Math.PI * 2 / w);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        waterHeight = waterHeight + 10;
        float startY;
        canvas.save();

        if (System.currentTimeMillis() - startTime < 100) {
            try {
                Thread.sleep(100 + startTime - System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        double temp = Math.toDegrees(speed);
        speed++;

        mPaint.setColor(secondWaveColor);
        for (int i = 0; i < mWidth; i++) {
            startY = (float) (waveHeight * Math.sin(w * i + temp + 8) + mHeight - waveHeight - waterHeight);
            canvas.drawLine(i, startY, i, mHeight, mPaint);
        }
        mPaint.setColor(waveColor);
        for (int i = 0; i < mWidth; i++) {
            startY = (float) (waveHeight * Math.sin(w * i + temp) + mHeight - waveHeight - waterHeight);
            canvas.drawLine(i, startY, i, mHeight, mPaint);
        }
        Log.i(TAG, "waterHeight : " + waterHeight);
        canvas.restore();
        invalidate();
        startTime = System.currentTimeMillis();
    }

    public void setWaterHeight(float waterHeight) {
        this.waterHeight = waterHeight;
    }

    public float getWaterHeight() {
        return waterHeight;
    }
}
