<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Selenium;
using Signum.Engine.Basics;
using Signum.Entities;
using Signum.Entities.Reflection;
using Signum.Utilities;
using Signum.Utilities.Reflection;

namespace Signum.Web.Selenium
{

    public class BaseLineProxy
    {
        public ISelenium Selenium { get; private set; }

        public string Prefix { get; private set; }

        public PropertyRoute Route { get; private set; }

        public BaseLineProxy(ISelenium selenium, string prefix, PropertyRoute route)
        {
            this.Selenium = selenium;
            this.Prefix = prefix;
            this.Route = route;
        }

        protected static string ToVisible(bool visible)
        {
            return visible ? "visible" : "not visible";
        }
    }

    public class ValueLineProxy : BaseLineProxy
    {
        public ValueLineProxy(ISelenium selenium, string prefix, PropertyRoute route)
            : base(selenium, prefix, route)
        {
        }

        public string StringValue
        {
            get
            {
                if (Selenium.IsElementPresent("jq=input:checkbox#{0}".Formato(Prefix)))
                    return Selenium.IsChecked(Prefix).ToString();

                return Selenium.GetValue(Prefix);
            }

            set
            {
                if (Selenium.IsElementPresent("jq=input:checkbox#{0}".Formato(Prefix)))
                    Selenium.SetChecked(Prefix, bool.Parse(value));
                else
                    Selenium.Type(Prefix, value);
            }
        }


        public object Value
        {
            get { return GetValue(Route.Type); }
            set { SetValue(value, Reflector.FormatString(Route)); }
        }

        public object GetValue(Type type)
        {
            return ReflectionTools.Parse(StringValue, type);
        }

        public void SetValue(object value, string format)
        {
            StringValue = value == null ? null :
                    value is IFormattable ? ((IFormattable)value).ToString(format, null) :
                    value.ToString();
        }
    }

    public abstract class EntityBaseProxy : BaseLineProxy
    {
        public EntityBaseProxy(ISelenium selenium, string prefix, PropertyRoute route)
            : base(selenium, prefix, route)
        {
        }

        public string CreateLocator
        {
            get { return "jq=#{0}_btnCreate".Formato(Prefix); }
        }

        public void Create()
        {
            Selenium.Click(CreateLocator);
        }

        public void CreateImplementations(Type selectType)
        {
            Create();

            string newPrefix = ChooseType(selectType, NewIndex());
        }

        public virtual int? NewIndex()
        {
            return null;
        }

        public PopupControl<T> CreatePopup<T>() where T : ModifiableEntity
        {
            var index = NewIndex();

            Create();

            string newPrefix = ChooseType(typeof(T), index);

            PropertyRoute route = index == null ? this.Route : this.Route.Add("Item");

            return new PopupControl<T>(this.Selenium, newPrefix, route);
        }

        public string ViewLocator
        {
            get { return "jq=#{0}_btnView".Formato(Prefix); }
        }

        protected PopupControl<T> ViewPopup<T>(int? index) where T : ModifiableEntity
        {
            string newPrefix = Prefix + (index == null ? "" : ("_" + index));

            PropertyRoute route = index == null ? this.Route : this.Route.Add("Item");

            return new PopupControl<T>(this.Selenium, newPrefix, route);
        }

        public string FindLocator
        {
            get { return "jq=#{0}_btnFind".Formato(Prefix); }
        }

        public string RemoveLocator
        {
            get { return "jq=#{0}_btnRemove".Formato(Prefix); }
        }

        public void Remove()
        {
            Selenium.Click(RemoveLocator);
        }

        public SearchPopupProxy Find(Type selectType = null)
        {
            Selenium.Click(FindLocator);

            ChooseType(selectType, null);

            return new SearchPopupProxy(Selenium, Prefix);
        }

        private string ChooseType(Type selectType, int? index)
        {
            string newPrefix = Prefix + (index == null ? "" : ("_" + index));

            Selenium.Wait(() => Popup.IsPopupVisible(Selenium, newPrefix) || Popup.IsPopupVisible(Selenium, Prefix), 
                () => "Popup {0} or {1} to be visible".Formato(newPrefix, Prefix));

            if (selectType == null)
            {
                if (ChooserPopup.IsChooser(Selenium, Prefix))<commit_msg>Fix LineProxies and visible buttons<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Selenium;
using Signum.Engine.Basics;
using Signum.Entities;
using Signum.Entities.Reflection;
using Signum.Utilities;
using Signum.Utilities.Reflection;

namespace Signum.Web.Selenium
{

    public class BaseLineProxy
    {
        public ISelenium Selenium { get; private set; }

        public string Prefix { get; private set; }

        public PropertyRoute Route { get; private set; }

        public BaseLineProxy(ISelenium selenium, string prefix, PropertyRoute route)
        {
            this.Selenium = selenium;
            this.Prefix = prefix;
            this.Route = route;
        }

        protected static string ToVisible(bool visible)
        {
            return visible ? "visible" : "not visible";
        }
    }

    public class ValueLineProxy : BaseLineProxy
    {
        public ValueLineProxy(ISelenium selenium, string prefix, PropertyRoute route)
            : base(selenium, prefix, route)
        {
        }

        public string StringValue
        {
            get
            {
                if (Selenium.IsElementPresent("jq=input:checkbox#{0}".Formato(Prefix)))
                    return Selenium.IsChecked(Prefix).ToString();

                return Selenium.GetValue(Prefix);
            }

            set
            {
                if (Selenium.IsElementPresent("jq=input:checkbox#{0}".Formato(Prefix)))
                    Selenium.SetChecked(Prefix, bool.Parse(value));
                else
                    Selenium.Type(Prefix, value);
            }
        }


        public object Value
        {
            get { return GetValue(Route.Type); }
            set { SetValue(value, Reflector.FormatString(Route)); }
        }

        public object GetValue(Type type)
        {
            return ReflectionTools.Parse(StringValue, type);
        }

        public void SetValue(object value, string format)
        {
            StringValue = value == null ? null :
                    value is IFormattable ? ((IFormattable)value).ToString(format, null) :
                    value.ToString();
        }
    }

    public abstract class EntityBaseProxy : BaseLineProxy
    {
        public EntityBaseProxy(ISelenium selenium, string prefix, PropertyRoute route)
            : base(selenium, prefix, route)
        {
        }

        public string CreateLocator
        {
            get { return "jq=#{0}_btnCreate:visible".Formato(Prefix); }
        }

        public void Create()
        {
            Selenium.Click(CreateLocator);
        }

        public void CreateImplementations(Type selectType)
        {
            Create();

            string newPrefix = ChooseType(selectType, NewIndex());
        }

        public virtual int? NewIndex()
        {
            return null;
        }

        public PopupControl<T> CreatePopup<T>() where T : ModifiableEntity
        {
            var index = NewIndex();

            Create();

            string newPrefix = ChooseType(typeof(T), index);

            PropertyRoute route = index == null ? this.Route : this.Route.Add("Item");

            return new PopupControl<T>(this.Selenium, newPrefix, route);
        }

        public string ViewLocator
        {
            get { return "jq=#{0}_btnView:visible".Formato(Prefix); }
        }

        protected PopupControl<T> ViewPopup<T>(int? index) where T : ModifiableEntity
        {
            string newPrefix = Prefix + (index == null ? "" : ("_" + index));

            PropertyRoute route = index == null ? this.Route : this.Route.Add("Item");

            return new PopupControl<T>(this.Selenium, newPrefix, route);
        }

        public string FindLocator
        {
            get { return "jq=#{0}_btnFind:visible".Formato(Prefix); }
        }

        public string RemoveLocator
        {
            get { return "jq=#{0}_btnRemove:visible".Formato(Prefix); }
        }

        public void Remove()
        {
            Selenium.Click(RemoveLocator);
        }

        public SearchPopupProxy Find(Type selectType = null)
        {
            Selenium.Click(FindLocator);

            ChooseType(selectType, null);

            return new SearchPopupProxy(Selenium, Prefix);
        }

        private string ChooseType(Type selectType, int? index)
        {
            string newPrefix = Prefix + (index == null ? "" : ("_" + index));

            Selenium.Wait(() => Popup.IsPopupVisible(Selenium, newPrefix) || Popup.IsPopupVisible(Selenium, Prefix), 
                () => "Popup {0} or {1} to be visible".Formato(newPrefix, Prefix));

            if (selectType == null)
            {
                if (ChooserPopup.IsChooser(Selenium, Prefix))
{'fraction_non_alphanumeric': 0.09907834101382489, 'fraction_numerical': 0.0016757436112274822, 'mean_word_length': 1.8110097144539299, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, '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': '5554084', 'n_tokens_mistral': 2604, 'n_tokens_neox': 2491, 'n_words': 571}