using PlantBox.Client.Models;
using PlantBox.Shared.Communication.Commands;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Xamarin.Forms;

namespace PlantBox.Client.Converters
{
    class CaptorValueToDoubleConverter : IValueConverter
    {
        const double HUMIDITY_UPPER_BOUND = 100.0;
        const double HUMIDITY_LOWER_BOUND = 0.0;
        const double LUMINOSITY_UPPER_BOUND = 1200.0;
        const double LUMINOSITY_LOWER_BOUND = 0.0;
        const double TEMPERATURE_UPPER_BOUND = 50.0;
        const double TEMPERATURE_LOWER_BOUND = -20.0;
        const double TANK_UPPER_BOUND = 100.0;
        const double TANK_LOWER_BOUND = 0.0;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double d && parameter is CaptorType type)
            {
                double upperBound;
                double lowerBound;

                switch (type)
                {
                    case CaptorType.Humidity:
                        upperBound = HUMIDITY_UPPER_BOUND;
                        lowerBound = HUMIDITY_LOWER_BOUND;
                        break;
                    case CaptorType.Luminosity:
                        upperBound = LUMINOSITY_UPPER_BOUND;
                        lowerBound = LUMINOSITY_LOWER_BOUND;
                        break;
                    case CaptorType.Temperature:
                        upperBound = TEMPERATURE_UPPER_BOUND;
                        lowerBound = TEMPERATURE_LOWER_BOUND;
                        break;
                    case CaptorType.Tank:
                        upperBound = TANK_UPPER_BOUND;
                        lowerBound = TANK_LOWER_BOUND;
                        break;
                    default:
                        throw new InvalidOperationException("How did you just got here?");
                }

                return (d - lowerBound) / (upperBound - lowerBound);
            }

            throw new ArgumentException("Invalid source or argument");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
