package com.BV.LinearGradient;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.Log;
import android.view.View;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.uimanager.CatalystStylesDiffMap;


public class LinearGradientView extends View {

    private Paint mPaint;
    private LinearGradient mShader;
    private float[] mLocations;
    private int[] mStartPos;
    private int[] mEndPos;
    private int[] mColors;
    private int[] mSize;

    public LinearGradientView(Context context, CatalystStylesDiffMap props) {
        super(context, null);

        mPaint = new Paint();

        ReadableArray colors = props.getArray("colors");

        //If we managed to get here and not get colors... give up.
        assert colors != null;
        mColors = new int[colors.size()];
        for (int i=0; i < mColors.length; i++)
        {
            mColors[i] = colors.getInt(i);
        }


        try {
            ReadableArray locations = props.getArray("locations");
            assert locations != null;
            mLocations= new float[locations.size()];
            for (int i=0; i < mLocations.length; i++)
            {
                mLocations[i] = (float) locations.getDouble(i);
            }
        } catch (Exception e) {
            mLocations = null;
        }

        try {
          ReadableArray startPos = props.getArray("start");
          assert startPos != null;
          mStartPos = new int[]{startPos.getInt(0), startPos.getInt(1)};
        } catch (Exception e) {
          mStartPos = new int[]{0,0};
        }


        try {
            ReadableArray endPos = props.getArray("end");
            assert endPos != null;
            mEndPos= new int[]{endPos.getInt(0), endPos.getInt(1)};
        } catch (Exception e) {
          //default to full height.
            mEndPos = new int[]{0, getMeasuredHeight()};
        }
        mSize = new int[]{0, 0};
        drawGradient();
    }

    public void updateStartPosition(ReadableArray startPos) {
      int[] _startPos;
      try {
        assert startPos != null;
        _startPos= new int[]{startPos.getInt(0), startPos.getInt(1)};
      } catch (Exception e) {
        _startPos = new int[]{0,0};
      }
      mStartPos = _startPos;
      drawGradient();
    }


    public void updateEndPosition(ReadableArray endPos) {
      int[] _endPos;
      try {
        assert endPos != null;
        _endPos= new int[]{endPos.getInt(0), endPos.getInt(1)};
      } catch (Exception e) {
        _endPos = new int[]{0,0};
      }
      mEndPos = _endPos;
      drawGradient();
    }


    public void updateColors(ReadableArray colors){
        int[] _colors = new int[colors.size()];
        for (int i=0; i < _colors.length; i++)
        {
            _colors[i] = colors.getInt(i);
        }
        mColors = _colors;
        drawGradient();
    }

    public void updateLocations(ReadableArray locations){
        float[] _locations;
        try {
            assert locations != null;
            _locations = new float[locations.size()];
            for (int i=0; i < _locations.length; i++)
            {
                _locations[i] = (float) locations.getDouble(i);
            }
        } catch (Exception e) {
            _locations = null;
        }
        mLocations = _locations;
        drawGradient();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mSize = new int[]{w, h};
        drawGradient();
    }

    private void drawGradient() {
        mShader = new LinearGradient(mStartPos[0] * mSize[0], mStartPos[1] * mSize[1], mEndPos[0] * mSize[0], mEndPos[1] * mSize[1], mColors, mLocations, Shader.TileMode.MIRROR);
        mPaint.setShader(mShader);
        invalidate();
    }

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

@@ -503,26 +503,28 @@
     private 
-in
+floa
 t%5B%5D mStartPo
@@ -534,26 +534,28 @@
     private 
-in
+floa
 t%5B%5D mEndPos;
@@ -1589,33 +1589,54 @@
 new 
-int%5B%5D%7BstartPos.getInt(0),
+float%5B%5D%7B(float) startPos.getDouble(0), (float)
  sta
@@ -1636,35 +1636,38 @@
 at) startPos.get
-Int
+Double
 (1)%7D;%0A        %7D 
@@ -1706,34 +1706,36 @@
 mStartPos = new 
-in
+floa
 t%5B%5D%7B0,0%7D;%0A      
@@ -1872,39 +1872,60 @@
 os= new 
-int%5B%5D%7BendPos.getInt(0),
+float%5B%5D%7B(float) endPos.getDouble(0), (float)
  endPos.
@@ -1919,35 +1919,38 @@
 loat) endPos.get
-Int
+Double
 (1)%7D;%0A        %7D 
@@ -2029,26 +2029,28 @@
 ndPos = new 
-in
+floa
 t%5B%5D%7B0, getMe
@@ -2204,26 +2204,28 @@
 os) %7B%0A      
-in
+floa
 t%5B%5D _startPo
@@ -2299,33 +2299,54 @@
 new 
-int%5B%5D%7BstartPos.getInt(0),
+float%5B%5D%7B(float) startPos.getDouble(0), (float)
  sta
@@ -2350,27 +2350,30 @@
 startPos.get
-Int
+Double
 (1)%7D;%0A      
@@ -2412,34 +2412,36 @@
 _startPos = new 
-in
+floa
 t%5B%5D%7B0,0%7D;%0A      
@@ -2561,26 +2561,28 @@
 os) %7B%0A      
-in
+floa
 t%5B%5D _endPos;
@@ -2650,31 +2650,52 @@
 new 
-int%5B%5D%7BendPos.getInt(0),
+float%5B%5D%7B(float) endPos.getDouble(0), (float)
  end
@@ -2701,19 +2701,22 @@
 dPos.get
-Int
+Double
 (1)%7D;%0A  
@@ -2761,26 +2761,28 @@
 ndPos = new 
-in
+floa
 t%5B%5D%7B0,0%7D;%0A  

handle floats