<commit_before>﻿﻿using Microsoft.Phone.Controls;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using WPPMM.DataModel;

namespace WPPMM.CameraManager
{
    public class ControlPanelManager
    {
        private CameraManager manager;
        private Status status;
        private StackPanel panel;

        public ControlPanelManager(StackPanel panel)
        {
            this.manager = CameraManager.GetInstance();
            this.status = manager.cameraStatus;
            this.panel = panel;
            manager.MethodTypesUpdateNotifer += () => { Initialize(); };
        }

        public bool IsShowing()
        {
            return panel.Visibility == Visibility.Visible;
        }

        public int ItemCount
        {
            get { return panel.Children.Count - 1; }
        }

        public void Show()
        {
            panel.Visibility = Visibility.Visible;
        }

        private void Initialize()
        {
            panel.Children.Clear();

            var title = new TextBlock
            {
                Text = Resources.AppResources.ControlPanel,
                HorizontalAlignment = HorizontalAlignment.Left,
                Style = Application.Current.Resources["PhoneTextLargeStyle"] as Style,
                Margin = new Thickness(-5, 15, 0, -10)
            };
            panel.Children.Add(title);

            if (status.IsSupported("setShootMode"))
            {
                panel.Children.Add(CreateStatusPanel("ShootMode", Resources.AppResources.ShootMode,
                     (sender, arg) =>
                     {
                         if (status.ShootModeInfo == null || status.ShootModeInfo.candidates == null)
                             return;
                         var selected = (sender as ListPicker).SelectedIndex;
                         manager.SetShootMode(status.ShootModeInfo.candidates[selected]);
                     }));
            }
            if (status.IsSupported("setSelfTimer"))
            {
                panel.Children.Add(CreateStatusPanel("SelfTimer", Resources.AppResources.SelfTimer,
                     (sender, arg) =>
                     {
                         if (status.SelfTimerInfo == null || status.SelfTimerInfo.candidates == null)
                             return;
                         var selected = (sender as ListPicker).SelectedIndex;
                         manager.SetSelfTimer(status.SelfTimerInfo.candidates[selected]);
                     }));
            }
            if (status.IsSupported("setPostviewImageSize"))
            {
                panel.Children.Add(CreateStatusPanel("PostviewSize", Resources.AppResources.Setting_PostViewImageSize,
                    (sender, arg) =>
                    {
                        if (status.PostviewSizeInfo == null || status.PostviewSizeInfo.candidates == null)
                            return;
                        var selected = (sender as ListPicker).SelectedIndex;
                        manager.SetPostViewImageSize(status.PostviewSizeInfo.candidates[selected]);
                    }));
            }

            panel.Children.Add(CreatePostviewSettingPanel(Resources.AppResources.PostviewTransferSetting,
                (sender, arg) =>
                {
                    var selected = (sender as ListPicker).SelectedIndex;
                    ApplicationSettings.GetInstance().IsPostviewTransferEnabled = (selected == 0);
                }));

            panel.Width = double.NaN;
        }

        public void Hide()
        {
            panel.Visibility = Visibility.Collapsed;
        }

        private StackPanel CreateStatusPanel(string id, string title, SelectionChangedEventHandler handler)
        {
            var child = CreatePanel(title);

            var statusbind = new Binding()
            {
                Source = status,
                Path = new PropertyPath("CpIsAvailable" + id),
                Mode = BindingMode.OneWay
            };
            var selectedbind = new Binding()
            {
                Source = status,
                Path = new PropertyPath("CpSelectedIndex" + id),
                Mode = BindingMode.TwoWay
            };
            var candidatesbind = new Binding()
            {
                Source = status,
                Path = new PropertyPath("CpCandidates" + id),
                Mode = BindingMode.OneWay
            };
            var picker = new ListPicker
            {
                SelectionMode = SelectionMode.Single,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(10, -5, 10, 0)
            };
            picker.SetBinding(ListPicker.IsEnabledProperty, statusbind);
            picker.SetBinding(ListPicker.ItemsSourceProperty, candidatesbind);
            picker.SetBinding(ListPicker.SelectedIndexProperty, selectedbind);
            picker.SelectionChanged += handler;

            child.Children.Add(picker);
            return child;
        }

        private StackPanel CreatePostviewSettingPanel(string title, SelectionChangedEventHandler handler)
        {
            var child = CreatePanel(title);

            var selectedbind = new Binding()
            {
                Source = ApplicationSettings.GetInstance(),
                Path = new PropertyPath("SelectedIndexPostviewTransferEnabled"),
                Mode = BindingMode.OneWay
            };

            var picker = new ListPicker
            {
                SelectionMode = SelectionMode.Single,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(10, -5, 10, 0)
            };
            picker.ItemsSource = ApplicationSettings.GetInstance().CandidatesPostviewTransferEnabled;
            picker.SetBinding(ListPicker.SelectedIndexProperty, selectedbind);

            child.Children.Add(picker);
            return child;
        }

        private StackPanel CreatePanel(string title)
        {
            var child = new StackPanel
            {
                HorizontalAlignment = HorizontalAlignment.Stretch
            };

            child.Children.Add(
                new TextBlock
                {
                    Text = title,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    Style = Application.Current.Resources["PhoneTextStyle"] as Style,
                    Margin = new Thickness(5, 20, 0, 0),
                    Width = 240
                }<commit_msg>Fix bug<commit_after>﻿﻿using Microsoft.Phone.Controls;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using WPPMM.DataModel;

namespace WPPMM.CameraManager
{
    public class ControlPanelManager
    {
        private CameraManager manager;
        private Status status;
        private StackPanel panel;

        public ControlPanelManager(StackPanel panel)
        {
            this.manager = CameraManager.GetInstance();
            this.status = manager.cameraStatus;
            this.panel = panel;
            manager.MethodTypesUpdateNotifer += () => { Initialize(); };
        }

        public bool IsShowing()
        {
            return panel.Visibility == Visibility.Visible;
        }

        public int ItemCount
        {
            get { return panel.Children.Count - 1; }
        }

        public void Show()
        {
            panel.Visibility = Visibility.Visible;
        }

        private void Initialize()
        {
            panel.Children.Clear();

            var title = new TextBlock
            {
                Text = Resources.AppResources.ControlPanel,
                HorizontalAlignment = HorizontalAlignment.Left,
                Style = Application.Current.Resources["PhoneTextLargeStyle"] as Style,
                Margin = new Thickness(-5, 15, 0, -10)
            };
            panel.Children.Add(title);

            if (status.IsSupported("setShootMode"))
            {
                panel.Children.Add(CreateStatusPanel("ShootMode", Resources.AppResources.ShootMode,
                     (sender, arg) =>
                     {
                         if (status.ShootModeInfo == null || status.ShootModeInfo.candidates == null)
                             return;
                         var selected = (sender as ListPicker).SelectedIndex;
                         manager.SetShootMode(status.ShootModeInfo.candidates[selected]);
                     }));
            }
            if (status.IsSupported("setSelfTimer"))
            {
                panel.Children.Add(CreateStatusPanel("SelfTimer", Resources.AppResources.SelfTimer,
                     (sender, arg) =>
                     {
                         if (status.SelfTimerInfo == null || status.SelfTimerInfo.candidates == null)
                             return;
                         var selected = (sender as ListPicker).SelectedIndex;
                         manager.SetSelfTimer(status.SelfTimerInfo.candidates[selected]);
                     }));
            }
            if (status.IsSupported("setPostviewImageSize"))
            {
                panel.Children.Add(CreateStatusPanel("PostviewSize", Resources.AppResources.Setting_PostViewImageSize,
                    (sender, arg) =>
                    {
                        if (status.PostviewSizeInfo == null || status.PostviewSizeInfo.candidates == null)
                            return;
                        var selected = (sender as ListPicker).SelectedIndex;
                        manager.SetPostViewImageSize(status.PostviewSizeInfo.candidates[selected]);
                    }));
            }

            panel.Children.Add(CreatePostviewSettingPanel(Resources.AppResources.PostviewTransferSetting,
                (sender, arg) =>
                {
                    var selected = (sender as ListPicker).SelectedIndex;
                    ApplicationSettings.GetInstance().IsPostviewTransferEnabled = (selected == 0);
                }));

            panel.Width = double.NaN;
        }

        public void Hide()
        {
            panel.Visibility = Visibility.Collapsed;
        }

        private StackPanel CreateStatusPanel(string id, string title, SelectionChangedEventHandler handler)
        {
            var child = CreatePanel(title);

            var statusbind = new Binding()
            {
                Source = status,
                Path = new PropertyPath("CpIsAvailable" + id),
                Mode = BindingMode.OneWay
            };
            var selectedbind = new Binding()
            {
                Source = status,
                Path = new PropertyPath("CpSelectedIndex" + id),
                Mode = BindingMode.TwoWay
            };
            var candidatesbind = new Binding()
            {
                Source = status,
                Path = new PropertyPath("CpCandidates" + id),
                Mode = BindingMode.OneWay
            };
            var picker = new ListPicker
            {
                SelectionMode = SelectionMode.Single,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(10, -5, 10, 0)
            };
            picker.SetBinding(ListPicker.IsEnabledProperty, statusbind);
            picker.SetBinding(ListPicker.ItemsSourceProperty, candidatesbind);
            picker.SetBinding(ListPicker.SelectedIndexProperty, selectedbind);
            picker.SelectionChanged += handler;

            child.Children.Add(picker);
            return child;
        }

        private StackPanel CreatePostviewSettingPanel(string title, SelectionChangedEventHandler handler)
        {
            var child = CreatePanel(title);

            var selectedbind = new Binding()
            {
                Source = ApplicationSettings.GetInstance(),
                Path = new PropertyPath("SelectedIndexPostviewTransferEnabled"),
                Mode = BindingMode.OneWay
            };

            var picker = new ListPicker
            {
                SelectionMode = SelectionMode.Single,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(10, -5, 10, 0)
            };
            picker.ItemsSource = ApplicationSettings.GetInstance().CandidatesPostviewTransferEnabled;
            picker.SetBinding(ListPicker.SelectedIndexProperty, selectedbind);
            picker.SelectionChanged += handler;

            child.Children.Add(picker);
            return child;
        }

        private StackPanel CreatePanel(string title)
        {
            var child = new StackPanel
            {
                HorizontalAlignment = HorizontalAlignment.Stretch
            };

            child.Children.Add(
                new TextBlock
                {
                    Text = title,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    Style = Application.Current.Resources["PhoneTextStyle"] as Style,
                    Margin = new Thickness(5, 20, 0, 0),
                    Width = 240
                }
{'fraction_non_alphanumeric': 0.08108930473147725, 'fraction_numerical': 0.004259850905218318, 'mean_word_length': 1.4890193108670957, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3515675', 'n_tokens_mistral': 3116, 'n_tokens_neox': 2841, 'n_words': 583}