using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace Elwig.Helpers.Weighing {
    public class AveryEventScale : Scale, IEventScale, IDisposable {

        public string Manufacturer => "Avery";
        public int InternalScaleNr => 1;
        public string Model { get; private set; }
        public string ScaleId { get; private set; }
        public bool IsReady { get; private set; }
        public bool HasFillingClearance { get; private set; }

        public event IEventScale.EventHandler<WeighingEventArgs> WeighingEvent;

        private bool IsRunning = true;
        private readonly Thread BackgroundThread;

        public AveryEventScale(string id, string model, string cnx, string? empty = null, string? filling = null, int? limit = null, string? log = null) :
            base(cnx, empty, filling, limit, log) {
            ScaleId = id;
            Model = model;
            IsReady = true;
            HasFillingClearance = false;
            BackgroundThread = new Thread(new ParameterizedThreadStart(BackgroundLoop));
            BackgroundThread.Start();
        }

        protected virtual void RaiseWeighingEvent(WeighingEventArgs evt) {
            WeighingEvent?.Invoke(this, evt);
        }

        public new void Dispose() {
            IsRunning = false;
            BackgroundThread.Interrupt();
            BackgroundThread.Join();
            base.Dispose();
            GC.SuppressFinalize(this);
        }

        protected async void BackgroundLoop(object? parameters) {
            while (IsRunning) {
                try {
                    var data = await Receive();
                    RaiseWeighingEvent(new WeighingEventArgs(data));
                } catch (Exception ex) {
                    MessageBox.Show($"Beim Wiegen ist ein Fehler Aufgetreten:\n\n{ex.Message}", "Waagenfehler",
                        MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        protected async Task<WeighingResult> Receive() {
            string? line = null;
            using (var reader = new StreamReader(Stream, Encoding.ASCII, false, -1, true)) {
                line = await reader.ReadLineAsync();
                if (LogPath != null) await File.AppendAllTextAsync(LogPath, $"{line}\r\n");
            }
            if (line == null || line.Length != 33 || line[0] != ' ' || line[9] != ' ' || line[15] != ' ' || line[20] != ' ' || line[32] != ' ') {
                throw new IOException($"Invalid event from scale: '{line}'");
            }

            var date    = line[ 1.. 9];
            var time    = line[10..15];
            var identNr = line[16..20].Trim();
            var netto   = line[21..30].Trim();
            var unit    = line[30..32];

            if (unit != "kg") {
                throw new IOException($"Unsupported unit in weighing event: '{unit}'");
            }

            identNr = identNr.Length > 0 && identNr != "0" ? identNr : null;
            var parsedDate = DateOnly.Parse(date);
            return new() {
                Weight = int.Parse(netto),
                WeighingId = identNr,
                FullWeighingId = identNr != null ? $"{parsedDate:yyyy-MM-dd}/{identNr}" : null,
                Date = parsedDate,
                Time = TimeOnly.Parse(time),
            };
        }
    }
}
