using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace CafeTerrace.Views.Controller
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Rating : ContentView
    {
        #region Rate
        private float _rate;
        public float Rate
        {
            get { return (float)this.GetValue(RateProperty); }
            set { this.SetValue(RateProperty, value); }
        }
        public static readonly BindableProperty RateProperty = BindableProperty.Create(
            nameof(Rate)
            , typeof(float)
            , typeof(Rating)
            , float.MinValue
            , BindingMode.TwoWay
            , null
            , RatePropertyChanged);
        private static void RatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control = (Rating)bindable;
            control.ChangeRating((int)Math.Round((float)newValue));
            control.starLabel.Text = ((float)newValue).ToString();
        }

        #endregion
        public Rating()
        {
            InitializeComponent();
        }
        public void ChangeRating(int rate)
        {
            for (int i = 0; i < rate; i++)
            {
                (starStack.Children[i] as Label).TextColor = (Color)Application.Current.Resources["PrimaryColor"];
            }

            for (int i = starStack.Children.Count - 1; i > rate - 1; i--)
            {
                (starStack.Children[i] as Label).TextColor = (Color)Application.Current.Resources["Gray-300"];
            }
        }
    }
}