using Android.Graphics;
using Com.Telerik.Widget.Chart.Visualization.CartesianChart;
using Com.Telerik.Widget.Chart.Visualization.CartesianChart.Series.Categorical;
using System.Linq;
using Xamarin.Forms.Platform.Android;
using CC = CartesianChartXF_1171097.Portable;
using XF = Xamarin.Forms;

[assembly: Xamarin.Forms.ResolutionGroupName(CC.GradientBarEffect.YourCompanyName)]
[assembly: Xamarin.Forms.ExportEffect(typeof(CartesianChartXF_1171097.Android.GradientBarEffect), CC.GradientBarEffect.GradientBarEffectName)]

namespace CartesianChartXF_1171097.Android
{
    public class GradientBarEffect : PlatformEffect
    {
        protected override void OnAttached()
        {
            CC.GradientBarEffect effect = (CC.GradientBarEffect)this.Element.Effects.First(e => e is CC.GradientBarEffect);
            var chart = (RadCartesianChartView)this.Control;

            for (int i = 0; i < chart.Series.Size(); i++)
            {
                BarSeries barSeries = chart.Series.Get(i) as BarSeries;
                if (barSeries != null)
                {
                    barSeries.DataPointRenderer = new GradientBarRenderer(ToColor(effect.StartColor), ToColor(effect.EndColor));
                }
            }
        }

        protected override void OnDetached()
        {
        }

        private Color ToColor(XF.Color color)
        {
            return new Color((byte)(color.R * 255), (byte)(color.G * 255), (byte)(color.B * 255), (byte)(color.A * 255));
        }
    }
}