using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization;
using System.Windows;
using System.Windows.Media;
using static QuikDawEditor.EDITING.MiscMethods;
using static QuikDawEditor.EDITING.StaticProperties;

namespace QuikDawEditor.EditingClasses;

public class AutoPoint : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged([CallerMemberName] string propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }

    double ellipseRadius = 5;

    private double _sourcePointms { get; set; }
    public double sourcePointms
    {
        get { return Math.Truncate(_sourcePointms); }
        set
        {
            _sourcePointms = Math.Truncate(value);
            NotifyPropertyChanged(nameof(AutoPointLeft));
        }
    }

    private float _AutoValue = 1;
    public float AutoValue
    {
        get { return _AutoValue; }
        //get { return Math.Max(minValue, Math.Min(maxValue, _AutoValue)); }
        set
        {
            _AutoValue = value;
            UpdateAutoValue();
        }
    }

    public void UpdateAutoValue()
    {
        NotifyPropertyChanged(nameof(AutoValue));
        NotifyPropertyChanged(nameof(AutoPointLeft));
        NotifyPropertyChanged(nameof(AutoPointTop));
        NotifyPropertyChanged(nameof(AutoValueString));
    }

    public void UpdateAutoString(string dispprefix) { dispPrefix = dispprefix; NotifyPropertyChanged(nameof(AutoValueString)); }

    string dispPrefix = "";
    public string AutoValueString
    {
        get { return dispPrefix + Math.Round(AutoValue, 2).ToString(); }

    }

    public float midValue { get { return valRange / 2 + minValue; } }
    public float valRange { get { return Math.Abs(maxValue - minValue); } }
    public float maxValue;
    public float minValue;

    private bool _IsLeftEdgeAutoPoint = false;
    public bool IsLeftEdgeAutoPoint { get { return _IsLeftEdgeAutoPoint; } set { _IsLeftEdgeAutoPoint = value; NotifyPropertyChanged(nameof(AutoPointFillBrush)); } }
    private bool _IsRightEdgeAutoPoint = false;
    public bool IsRightEdgeAutoPoint { get { return _IsRightEdgeAutoPoint; } set { _IsRightEdgeAutoPoint = value; NotifyPropertyChanged(nameof(AutoPointFillBrush)); } }
    public bool IsLeftOrRightEdgeAutoPoint { get { return IsRightEdgeAutoPoint | IsLeftEdgeAutoPoint; } }

    private bool _IsRecAnchorAutoPoint = false;
    public bool IsRecAnchorAutoPoint { get { return _IsRecAnchorAutoPoint; } set { _IsRecAnchorAutoPoint = value; NotifyPropertyChanged(nameof(AutoPointFillBrush)); } }

    public SolidColorBrush AutoPointFillBrush { get { return IsLeftOrRightEdgeAutoPoint ? Brushes.White : IsRecAnchorAutoPoint ? Brushes.LightGreen : Brushes.Red; } }

    private double _relStartOffsetMs = 0;
    [JsonIgnore(Condition = JsonIgnoreCondition.Always)]
    public double relStartOffsetMs
    {
        get { return Math.Truncate(_relStartOffsetMs); }
        set { _relStartOffsetMs = Math.Truncate(value); NotifyPropertyChanged(nameof(AutoPointLeft)); }
    }

    private double _AutoPointReverseTransformX = 1;
    [JsonIgnore(Condition = JsonIgnoreCondition.Always)]
    public double AutoPointReverseTransformX
    {
        get { return _AutoPointReverseTransformX; }
        set { _AutoPointReverseTransformX = value; NotifyPropertyChanged(nameof(AutoPointReverseTransformX)); }
    }

    private Visibility _AutoPointValVisibility = Visibility.Hidden;
    [JsonIgnore(Condition = JsonIgnoreCondition.Always)]
    public Visibility AutoPointValVisibility { get { return _AutoPointValVisibility; } set { _AutoPointValVisibility = value; NotifyPropertyChanged(nameof(AutoPointValVisibility)); } }

    public double APZIndex { get { return IsLeftOrRightEdgeAutoPoint ? 100000 : 1; } }

    public double AutoPointLeft { get { return MsecToPixels(sourcePointms) - ellipseRadius / editingProject.ViewXZoomFac; } }
    public double AutoPointTop { get { return Math.Abs(maxValue - _AutoValue) / valRange * (float)UnitHeight - (float)ellipseRadius; } }

}