<commit_before>﻿// Copyright 2014 The Rector & Visitors of the University of Virginia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Microcharts;
using Microcharts.Forms;
using Newtonsoft.Json;
using Sensus.UI.UiProperties;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms;

namespace Sensus.UI.Inputs
{
	public class ScoreInput : Input, IVariableDefiningInput
	{
		private string _definedVariable;
		private IEnumerable<Input> _inputs = new List<Input>();
		private Label _label;
		private Span _scoreSpan;
		private Span _maxScoreSpan;
		private DonutChart _chart;

		private static SKColor _scoreColor;
		private static SKColor _scoreRemainingColor;
		private static Color _scoreLabelColor;
		private static Color _scoreLabelDividerColor;
		private static Color _maxScoreLabelColor;

		static ScoreInput()
		{
			if (Application.Current.Resources.TryGetValue("ScoreColor", out object scoreColor))
			{
				_scoreColor = SKColor.Parse(((Color)scoreColor).ToHex());
			}
			else
			{
				_scoreColor = SKColor.Parse(Color.Accent.ToHex());
			}

			if (Application.Current.Resources.TryGetValue("ScoreRemainingColor", out object scoreRemainingColor))
			{
				_scoreRemainingColor = SKColor.Parse(((Color)scoreRemainingColor).ToHex());
			}
			else
			{
				_scoreRemainingColor = SKColor.Empty;
			}

			if (Application.Current.Resources.TryGetValue("ScoreLabelColor", out object scoreLabelColor))
			{
				_scoreLabelColor = (Color)scoreLabelColor;
			}
			else
			{
				_scoreLabelColor = Color.Default;
			}

			if (Application.Current.Resources.TryGetValue("ScoreLabelDividerColor", out object scoreLabelDividerColor))
			{
				_scoreLabelDividerColor = (Color)scoreLabelDividerColor;
			}
			else
			{
				_scoreLabelDividerColor = _scoreLabelColor;
			}

			if (Application.Current.Resources.TryGetValue("ScoreRemainingLabelColor", out object scoreRemainingLabelColor))
			{
				_maxScoreLabelColor = (Color)scoreRemainingLabelColor;
			}
			else
			{
				_maxScoreLabelColor = _scoreLabelColor;
			}
		}

		public override object Value
		{
			get
			{
				return _score;
			}
		}

		public override bool Enabled { get; set; }

		public override string DefaultName => "Score";

		[EntryStringUiProperty("Define Variable:", true, 2, false)]
		public string DefinedVariable
		{
			get
			{
				return _definedVariable;
			}
			set
			{
				_definedVariable = value?.Trim();
			}
		}

		[ListUiProperty("Score Method:", true, 21, new object[] { ScoreMethods.Total, ScoreMethods.Average }, false)]
		public override ScoreMethods ScoreMethod { get; set; } = ScoreMethods.Total;

		[OnOffUiProperty("Show Score as Percent:", true, 22)]
		public bool ShowScoreAsPercent { get; set; }

		[HiddenUiProperty]
		public override float CorrectScore { get; set; }

		[HiddenUiProperty]
		public override object CorrectValue { get; set; }

		[HiddenUiProperty]
		public override int? Retries => 0;

		[JsonIgnore]
		public IEnumerable<Input> Inputs
		{
			get
			{
				return _inputs;
			}
			set
			{
				// remove the ScoreChanged event from each of the original inputs.
				foreach (Input input in _inputs)
				{
					input.PropertyChanged -= ScoreChanged;
				}

				if (string.IsNullOrWhiteSpace(ScoreGroup))
				{
					_inputs = value.OfType<ScoreInput>().Where(x => string.IsNullOrWhiteSpace(x.ScoreGroup) == false);
				}
				else
				{
					_inputs = value.Where(x => x is ScoreInput == false);
				}

				foreach (Input input in _inputs)
				{
					input.PropertyChanged += ScoreChanged;
				}
<commit_msg>scoreinput - refactor<commit_after>﻿// Copyright 2014 The Rector & Visitors of the University of Virginia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Microcharts;
using Microcharts.Forms;
using Newtonsoft.Json;
using Sensus.UI.UiProperties;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms;

namespace Sensus.UI.Inputs
{
	public class ScoreInput : Input, IVariableDefiningInput
	{
		private string _definedVariable;
		private List<Input> _inputs;
		private Label _label;
		private Span _scoreSpan;
		private Span _maxScoreSpan;
		private DonutChart _chart;

		private static SKColor _scoreColor;
		private static SKColor _scoreRemainingColor;
		private static Color _scoreLabelColor;
		private static Color _scoreLabelDividerColor;
		private static Color _maxScoreLabelColor;

		static ScoreInput()
		{
			if (Application.Current.Resources.TryGetValue("ScoreColor", out object scoreColor))
			{
				_scoreColor = SKColor.Parse(((Color)scoreColor).ToHex());
			}
			else
			{
				_scoreColor = SKColor.Parse(Color.Accent.ToHex());
			}

			if (Application.Current.Resources.TryGetValue("ScoreRemainingColor", out object scoreRemainingColor))
			{
				_scoreRemainingColor = SKColor.Parse(((Color)scoreRemainingColor).ToHex());
			}
			else
			{
				_scoreRemainingColor = SKColor.Empty;
			}

			if (Application.Current.Resources.TryGetValue("ScoreLabelColor", out object scoreLabelColor))
			{
				_scoreLabelColor = (Color)scoreLabelColor;
			}
			else
			{
				_scoreLabelColor = Color.Default;
			}

			if (Application.Current.Resources.TryGetValue("ScoreLabelDividerColor", out object scoreLabelDividerColor))
			{
				_scoreLabelDividerColor = (Color)scoreLabelDividerColor;
			}
			else
			{
				_scoreLabelDividerColor = _scoreLabelColor;
			}

			if (Application.Current.Resources.TryGetValue("ScoreRemainingLabelColor", out object scoreRemainingLabelColor))
			{
				_maxScoreLabelColor = (Color)scoreRemainingLabelColor;
			}
			else
			{
				_maxScoreLabelColor = _scoreLabelColor;
			}
		}

		public ScoreInput()
		{
			_inputs = new List<Input>();
		}

		public override object Value
		{
			get
			{
				return _score;
			}
		}

		public override bool Enabled { get; set; }

		public override string DefaultName => "Score";

		[EntryStringUiProperty("Define Variable:", true, 2, false)]
		public string DefinedVariable
		{
			get
			{
				return _definedVariable;
			}
			set
			{
				_definedVariable = value?.Trim();
			}
		}

		[ListUiProperty("Score Method:", true, 21, new object[] { ScoreMethods.Total, ScoreMethods.Average }, false)]
		public override ScoreMethods ScoreMethod { get; set; } = ScoreMethods.Total;

		[OnOffUiProperty("Show Score as Percent:", true, 22)]
		public bool ShowScoreAsPercent { get; set; }

		[HiddenUiProperty]
		public override float CorrectScore { get; set; }

		[HiddenUiProperty]
		public override object CorrectValue { get; set; }

		[HiddenUiProperty]
		public override int? Retries => 0;

		[JsonIgnore]
		public IEnumerable<Input> Inputs
		{
			get
			{
				return _inputs;
			}
			set
			{
				// remove the ScoreChanged event from each of the original inputs.
				foreach (Input input in _inputs)
				{
					input.PropertyChanged -= ScoreChanged;
				}

				if (string.IsNullOrWhiteSpace(ScoreGroup))
				{
					_inputs = value.Where(x => x is ScoreInput && string.IsNullOrWhiteSpace(x.ScoreGroup) == false).ToList();
				}
				else
				{
					_inputs = value.Where(x => x is ScoreInput == false).ToList();
				}

				foreach (Input input in _inputs)
				{
					input.PropertyChanged += ScoreChanged;
				}

{'fraction_non_alphanumeric': 0.09384057971014492, 'fraction_numerical': 0.003381642512077295, 'mean_word_length': 3.667981961668546, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4424051', 'n_tokens_mistral': 2911, 'n_tokens_neox': 2400, 'n_words': 671}