Apricot.Balloon.OnTick C# (CSharp) Method

OnTick() private method

private OnTick ( object sender, EventArgs e ) : void
sender object
e System.EventArgs
return void
        private void OnTick(object sender, EventArgs e)
        {
            if (sender == this.waitTimer && !this.isPinned && this.targetOpacity > 0 && this.targetScaleX > 0 && this.targetScaleY > 0)
            {
                this.sourceOpacity = this.Canvas.Opacity;
                this.targetOpacity = 0;
                this.sourceScaleX = this.ScaleTransform.ScaleX;
                this.sourceScaleY = this.ScaleTransform.ScaleY;
                this.targetScaleX = this.targetScaleY = 0;
                this.popupStep = 0;
                this.waitTimer.Stop();
            }
            else if (sender == this.messageTypeTimer && this.messageCollection.Count > 0 && this.historyPoint.HasValue && this.historyPoint.Value < this.messageCollection.Count)
            {
                Message message = this.messageCollection[this.historyPoint.Value];
                bool stopRequired = true;

                if (this.messageBuffer.Length < this.targetMessageLength)
                {
                    int index = this.messageBuffer.Length;
                    string text = message.Text;

                    if (index < text.Length)
                    {
                        int width = text.Length / 2 + text.Length % 2;
                        int length = text.Length;
                        int start = text.LastIndexOf(Environment.NewLine, index, StringComparison.Ordinal);
                        int end = text.IndexOf(Environment.NewLine, index, StringComparison.Ordinal);

                        if (start >= 0 || end >= 0)
                        {
                            if (start < 0)
                            {
                                start = 0;
                            }
                            else
                            {
                                start += Environment.NewLine.Length;
                            }

                            if (end < 0)
                            {
                                end = text.Length;
                            }

                            width = start + (end - start) / 2 + (end - start) % 2;
                            length = end;
                        }

                        if (this.randomMessageLength >= width)
                        {
                            if (text.IndexOf(Environment.NewLine, this.messageBuffer.Length, StringComparison.Ordinal) == this.messageBuffer.Length)
                            {
                                this.messageBuffer.Append(Environment.NewLine);
                            }
                            else
                            {
                                this.messageBuffer.Append(text[index]);
                            }

                            if (this.messageBuffer.Length != this.targetMessageLength)
                            {
                                stopRequired = false;
                            }
                        }
                        else
                        {
                            stopRequired = false;
                        }

                        if (this.randomMessageLength < length)
                        {
                            if (text.IndexOf(Environment.NewLine, this.randomMessageLength, StringComparison.Ordinal) == this.randomMessageLength)
                            {
                                this.randomMessageLength += Environment.NewLine.Length;
                            }
                            else
                            {
                                this.randomMessageLength++;
                            }
                        }
                    }
                    else
                    {
                        stopRequired = false;
                    }
                }
                else if (this.randomMessageLength > this.targetMessageLength)
                {
                    int index = this.randomMessageLength - 1;
                    string text = message.Text;

                    if (index < text.Length)
                    {
                        int start = text.LastIndexOf(Environment.NewLine, index, StringComparison.Ordinal);
                        int end = text.IndexOf(Environment.NewLine, index, StringComparison.Ordinal);

                        if (start < 0)
                        {
                            start = 0;
                        }
                        else
                        {
                            start += Environment.NewLine.Length;
                        }

                        if (end < 0)
                        {
                            end = text.Length;
                        }

                        if (this.messageBuffer.Length <= start + (end - start) / 2)
                        {
                            if (this.randomMessageLength - Environment.NewLine.Length >= 0 && text.LastIndexOf(Environment.NewLine, this.randomMessageLength, StringComparison.Ordinal) == this.randomMessageLength - Environment.NewLine.Length)
                            {
                                this.randomMessageLength -= Environment.NewLine.Length;
                            }
                            else
                            {
                                this.randomMessageLength--;
                            }
                        }

                        if (this.messageBuffer.Length > start)
                        {
                            if (this.messageBuffer.Length - Environment.NewLine.Length >= 0 && text.LastIndexOf(Environment.NewLine, this.messageBuffer.Length, StringComparison.Ordinal) == this.messageBuffer.Length - Environment.NewLine.Length)
                            {
                                this.messageBuffer.Remove(this.messageBuffer.Length - Environment.NewLine.Length, Environment.NewLine.Length);
                            }
                            else
                            {
                                this.messageBuffer.Remove(this.messageBuffer.Length - 1, 1);
                            }
                        }
                    }

                    stopRequired = false;
                }

                if (stopRequired)
                {
                    this.messageTypeTimer.Stop();
                }
            }
            else if (sender == this.switchTimer && this.inspectorEntry != null)
            {
                const double space = 10;
                double x = 0;

                if (this.inspectorEntry.HasMultipleImages)
                {
                    this.inspectorEntry.NextImage();

                    if (!this.imageDictionary.ContainsKey(this.inspectorEntry.Image) && !this.imageUriHashSet.Contains(this.inspectorEntry.Image))
                    {
                        this.imageUriHashSet.Add(this.inspectorEntry.Image);

                        UpdateImage(this.inspectorEntry.Image, false);
                    }

                    this.imageUriQueue.Enqueue(this.inspectorEntry.Image);
                }

                foreach (string tag in this.inspectorEntry.Tags)
                {
                    Dictionary<int, int> dictionary = new Dictionary<int, int>();
                    StringBuilder lineStringBuilder = new StringBuilder();
                    bool isCirculatable = false;

                    foreach (System.Text.RegularExpressions.Match match in System.Text.RegularExpressions.Regex.Matches(tag, @"[\p{IsBasicLatin}-[\s]]+\s?"))
                    {
                        dictionary.Add(match.Index, match.Length);
                    }

                    if (x + space + Math.Ceiling(new FormattedText(tag, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.linkBrush).WidthIncludingTrailingWhitespace) > this.baseWidth - 74 && x != 0)
                    {
                        this.circulationQueue.Enqueue(1);

                        break;
                    }

                    for (int i = 0; i < tag.Length; i++)
                    {
                        int length;

                        if (dictionary.TryGetValue(i, out length) && x + space + Math.Ceiling(new FormattedText(String.Concat(lineStringBuilder.ToString(), tag.Substring(i, length)), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.linkBrush).WidthIncludingTrailingWhitespace) > this.baseWidth - 74 && lineStringBuilder.Length > 0)
                        {
                            isCirculatable = true;

                            break;
                        }

                        lineStringBuilder.Append(tag[i]);

                        if (lineStringBuilder.ToString().EndsWith(Environment.NewLine, StringComparison.Ordinal))
                        {
                            isCirculatable = true;

                            break;
                        }
                        else if (lineStringBuilder.Length > 0 && x + space + Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.linkBrush).WidthIncludingTrailingWhitespace) > this.baseWidth - 74)
                        {
                            isCirculatable = true;

                            break;
                        }
                    }

                    if (isCirculatable)
                    {
                        this.circulationQueue.Enqueue(1);

                        break;
                    }

                    if (lineStringBuilder.Length > 0)
                    {
                        x += space + Math.Ceiling(new FormattedText(lineStringBuilder.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch), this.FontSize, this.linkBrush).WidthIncludingTrailingWhitespace);
                    }
                }
            }
        }