<commit_before>﻿using Alarmy.Common;
using Alarmy.Infrastructure;
using Castle.Core.Logging;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace Alarmy.Views
{
    internal partial class MainForm : AppBar
    {
        private readonly SoundPlayer soundPlayer;
        private readonly IAlarmService alarmService;
        private readonly int alarmListGroupInterval;

        private delegate void ShowDelegate(AlarmStatusChangedEventArgs args);

        private ILogger logger = NullLogger.Instance;
        private Point clickedPosition;
        private bool WasRegistered;

        public ILogger Logger
        {
            get { return this.logger; }
            set { this.logger = value; }
        }

        public MainForm(IAlarmService alarmService, Settings settings)
        {
            this.alarmListGroupInterval = settings.AlarmListGroupInterval;
            var alarmSoundPath = settings.AlarmSoundFile;
            this.soundPlayer = new SoundPlayer(alarmSoundPath);
            this.alarmService = alarmService;
            this.alarmService.Interval = settings.CheckInterval;
            this.alarmService.AlarmStatusChanged += alarmService_AlarmStatusChanged;

            InitializeComponent();
            listView1.DoubleBuffered(true);
            notifyIcon1.Icon = Icon;

            soundToolStripMenuItem.Checked = settings.EnableSound;
            popupOnAlarmMenuItem.Checked = !settings.DontPopup;
            timer1.Interval = settings.RefreshInterval;
            timer1.Enabled = true;

            if (settings.StartHidden)
                hideToolStripMenuItem_Click(null, null);
        }

        private void AlarmsForm_Load(object sender, EventArgs e)
        {
            alarmService.Start();
            RefreshList();
            RegisterBar();
        }

        private void AlarmsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            RegisterBar();
        }

        private void alarmService_AlarmStatusChanged(object sender, AlarmStatusChangedEventArgs e)
        {
            Invoke(new ShowDelegate(AlarmStatusChanged), e);
        }

        private void AlarmStatusChanged(AlarmStatusChangedEventArgs args)
        {            
            if ((args.Alarm.Status == AlarmStatus.Ringing || args.Alarm.Status == AlarmStatus.Missed) && !Visible)
            {
                Show();
            }

            RefreshList();
        }

        private void CheckForAlarmSound(IEnumerable<IAlarm> alarms)
        {
            if (alarms.Any(x => x.Status == AlarmStatus.Ringing && !x.IsHushed))
            {
                if (!this.soundPlayer.IsPlaying)
                    this.soundPlayer.Play();
            }
            else
            {
                if (this.soundPlayer.IsPlaying)
                    this.soundPlayer.Stop();
            }
        }

        private void RefreshList()
        {
            if (listView1.SelectedItems.Count > 0 && itemContext.Visible)
            {
                return;
            }
            listView1.BeginUpdate();
            listView1.Clear();
            listView1.View = View.Details;
            listView1.ShowGroups = true;
            listView1.Columns.Add("Title");
            listView1.Columns.Add("Time");
            listView1.Columns.Add("Status");

            var alarms = alarmService.List().ToArray();
            alarms.OfType<Alarm>()
                                    .Where(x => x.IsWorthShowing)
                                    .OrderBy(x => x.Time)
                                    .GroupBy(x => x.Time.RoundUp(TimeSpan.FromMinutes(this.alarmListGroupInterval)))
                                    .ToList().ForEach(x =>
            {
                var group = new ListViewGroup(x.Key.ToString(), String.Format("{0}-{1}", x.Key.AddMinutes(-this.alarmListGroupInterval).ToShortTimeString(), x.Key.ToShortTimeString()));
                listView1.Groups.Add(group);
                listView1.Items.AddRange(x.Select(item => new ListViewItem(new [] { item.Title, item.Time.ToShortTimeString(), item.Status.ToString() }, group) { Tag = item, BackColor = GetColor(item), ToolTipText = item.CancelReason }).ToArray());
            });

            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            listView1.EndUpdate();

            if (this.soundToolStripMenuItem.Checked)
                CheckForAlarmSound(alarms);
        }

        #region Window Resize, Move
        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            if (clickedPosition != Point.Empty && e.Button.HasFlag(MouseButtons.Left))
            {
                Left += e.Location.X - clickedPosition.X;
                Top += e.Location.Y - clickedPosition.Y;
            }
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {<commit_msg>Show if an alarm is hushed<commit_after>﻿using Alarmy.Common;
using Alarmy.Infrastructure;
using Castle.Core.Logging;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace Alarmy.Views
{
    internal partial class MainForm : AppBar
    {
        private readonly SoundPlayer soundPlayer;
        private readonly IAlarmService alarmService;
        private readonly int alarmListGroupInterval;

        private delegate void ShowDelegate(AlarmStatusChangedEventArgs args);

        private ILogger logger = NullLogger.Instance;
        private Point clickedPosition;
        private bool WasRegistered;

        public ILogger Logger
        {
            get { return this.logger; }
            set { this.logger = value; }
        }

        public MainForm(IAlarmService alarmService, Settings settings)
        {
            this.alarmListGroupInterval = settings.AlarmListGroupInterval;
            var alarmSoundPath = settings.AlarmSoundFile;
            this.soundPlayer = new SoundPlayer(alarmSoundPath);
            this.alarmService = alarmService;
            this.alarmService.Interval = settings.CheckInterval;
            this.alarmService.AlarmStatusChanged += alarmService_AlarmStatusChanged;

            InitializeComponent();
            listView1.DoubleBuffered(true);
            notifyIcon1.Icon = Icon;

            soundToolStripMenuItem.Checked = settings.EnableSound;
            popupOnAlarmMenuItem.Checked = !settings.DontPopup;
            timer1.Interval = settings.RefreshInterval;
            timer1.Enabled = true;

            if (settings.StartHidden)
                hideToolStripMenuItem_Click(null, null);
        }

        private void AlarmsForm_Load(object sender, EventArgs e)
        {
            alarmService.Start();
            RefreshList();
            RegisterBar();
        }

        private void AlarmsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            RegisterBar();
        }

        private void alarmService_AlarmStatusChanged(object sender, AlarmStatusChangedEventArgs e)
        {
            Invoke(new ShowDelegate(AlarmStatusChanged), e);
        }

        private void AlarmStatusChanged(AlarmStatusChangedEventArgs args)
        {            
            if ((args.Alarm.Status == AlarmStatus.Ringing || args.Alarm.Status == AlarmStatus.Missed) && !Visible)
            {
                Show();
            }

            RefreshList();
        }

        private void CheckForAlarmSound(IEnumerable<IAlarm> alarms)
        {
            if (alarms.Any(x => x.Status == AlarmStatus.Ringing && !x.IsHushed))
            {
                if (!this.soundPlayer.IsPlaying)
                    this.soundPlayer.Play();
            }
            else
            {
                if (this.soundPlayer.IsPlaying)
                    this.soundPlayer.Stop();
            }
        }

        private void RefreshList()
        {
            if (listView1.SelectedItems.Count > 0 && itemContext.Visible)
            {
                return;
            }
            listView1.BeginUpdate();
            listView1.Clear();
            listView1.View = View.Details;
            listView1.ShowGroups = true;
            listView1.Columns.Add("Title");
            listView1.Columns.Add("Time");
            listView1.Columns.Add("Status");

            var alarms = alarmService.List().ToArray();
            alarms.OfType<Alarm>()
                                    .Where(x => x.IsWorthShowing)
                                    .OrderBy(x => x.Time)
                                    .GroupBy(x => x.Time.RoundUp(TimeSpan.FromMinutes(this.alarmListGroupInterval)))
                                    .ToList().ForEach(x =>
            {
                var group = new ListViewGroup(x.Key.ToString(), String.Format("{0}-{1}", x.Key.AddMinutes(-this.alarmListGroupInterval).ToShortTimeString(), x.Key.ToShortTimeString()));
                listView1.Groups.Add(group);
                listView1.Items.AddRange(x.Select(item => new ListViewItem(new[] { item.Title, item.Time.ToShortTimeString(), AlarmStatus(item) }, group) { Tag = item, BackColor = GetColor(item), ToolTipText = item.CancelReason }).ToArray());
            });

            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            listView1.EndUpdate();

            if (this.soundToolStripMenuItem.Checked)
                CheckForAlarmSound(alarms);
        }

        private static string AlarmStatus(Alarm item)
        {
            return item.Status.ToString() + (item.IsHushed ? " (Hushed)" : string.Empty);
        }

        #region Window Resize, Move
        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            if (clickedPosition != Point.Empty && e.Button.HasFlag(MouseButtons.Left))
            {
                Left += e.Location.X - clickedPosition.X;
                Top += e.Location.Y - clickedPosition.Y;
            }
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
{'fraction_non_alphanumeric': 0.09195402298850575, 'fraction_numerical': 0.004161712247324614, 'mean_word_length': 1.8543552036199096, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3480363', 'n_tokens_mistral': 2620, 'n_tokens_neox': 2552, 'n_words': 471}