<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Signum.Utilities;
using Signum.Entities.Files;
using Signum.Entities;
using Signum.Web;
using Signum.Entities.Reflection;
using System.Linq.Expressions;
using Signum.Entities.Basics;
using Signum.Engine.Basics;
using Signum.Engine;
using Signum.Web.PortableAreas;

namespace Signum.Web.Files
{
    public static class FileLineHelper
    {
        

        public static MvcHtmlString FileLine<T, S>(this HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, S>> property)
            where S : IFile
        {
            return FileLineInternal<T, S>(helper, tc, property, null);
        }

        public static MvcHtmlString FileLine<T, S>(this HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, S>> property, Action<FileLine> settingsModifier)
            where S : IFile
        {
            return FileLineInternal<T, S>(helper, tc, property, settingsModifier);
        }

        public static MvcHtmlString FileLineLite<T, S>(this HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, Lite<S>>> property)
           where S : class, IFile, IEntity 
        {
            return FileLineInternal<T, Lite<S>>(helper, tc, property, null);
        }

        public static MvcHtmlString FileLineLite<T, S>(this HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, Lite<S>>> property, Action<FileLine> settingsModifier)
           where S : class, IFile, IEntity 
        {
            return FileLineInternal<T, Lite<S>>(helper, tc, property, settingsModifier);
        }

        static MvcHtmlString FileLineInternal<T, S>(HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, S>> property, Action<FileLine> settingsModifier)
        {
            TypeContext<S> context = Common.WalkExpression(tc, property);

            FileLine fl = new FileLine(context.Type, context.UntypedValue, context, "", context.PropertyRoute);

            EntityBaseHelper.ConfigureEntityBase(fl, fl.Type.CleanType());

            fl.Download = FilesClient.DownloadUrlConstructors.ContainsKey(context.Type.CleanType()) ? DownloadBehaviour.View : DownloadBehaviour.None;

            Common.FireCommonTasks(fl);

            if (settingsModifier != null)
                settingsModifier(fl);

            return helper.InternalFileLine(fl);
        }

        internal static MvcHtmlString InternalFileLine(this HtmlHelper helper, FileLine fileLine)
        {
            if (!fileLine.Visible)
                return MvcHtmlString.Empty;

            IFile value = fileLine.GetFileValue();

            HtmlStringBuilder sbg = new HtmlStringBuilder();

            using (sbg.SurroundLine(new HtmlTag("div").Id(fileLine.Prefix).Class("sf-field SF-control-container")))
            {
                sbg.AddLine(new HtmlTag("link").Attrs(new { rel = "stylesheet", type = "text/css", href = RouteHelper.New().Content("~/Files/Content/Files.css") }).ToHtmlSelf());

                if (value != null)
                    sbg.AddLine(helper.Div(fileLine.Compose(EntityBaseKeys.Entity), null, "", new Dictionary<string, object> { { "style", "display:none" } }));

                fileLine.ValueHtmlProps.AddCssClass("form-control");

                bool hasEntity = value != null && value.FileName.HasText();

                using (sbg.SurroundLine(new HtmlTag("div", fileLine.Compose("DivOld")).Attr("style", "display:" + (hasEntity ? "block" : "none"))))
                {
                    HtmlStringBuilder sb = new HtmlStringBuilder();
                    using (sb.SurroundLine(new HtmlTag("div", fileLine.Compose("inputGroup")).Class("input-group")))
                    {
                        if (fileLine.Download != DownloadBehaviour.None)
                        {
                            sb.AddLine(helper.Href(fileLine.Compose(EntityBaseKeys.Link),
                                value.Try(f => f.FileName),
                                hasEntity ? FilesClient.GetDownloadUrl(value) : null,
                                "Download",
                                "form-control file-control",
                                fileLine.Download == DownloadBehaviour.View ? null :
                                new Dictionary<string, object> { { "download", value.Try(f => f.FileName) } }));
                        }
                        else
                        {
                            sb.AddLine(helper.Span(fileLine.Compose(EntityBaseKeys.ToStr), value.Try(f => f.FileName) ?? "", "form-control file-control", null));
                        }

                        if (fileLine.Type.IsEmbeddedEntity())
                            sb.AddLine(helper.Hidden(fileLine.Compose(EntityBaseKeys.EntityState), value.Try(f => Navigator.Manager.SerializeEntity((ModifiableEntity)f))));
                        
                        using (sb.SurroundLine(new HtmlTag("span", fileLine.Compose("shownButton")).Class("input-group-btn")))
                        {
                            sb.AddLine(EntityButtonHelper.Remove(helper, fileLine, btn: true));<commit_msg>change FileLineHelper  title by the file name<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Signum.Utilities;
using Signum.Entities.Files;
using Signum.Entities;
using Signum.Web;
using Signum.Entities.Reflection;
using System.Linq.Expressions;
using Signum.Entities.Basics;
using Signum.Engine.Basics;
using Signum.Engine;
using Signum.Web.PortableAreas;

namespace Signum.Web.Files
{
    public static class FileLineHelper
    {
        

        public static MvcHtmlString FileLine<T, S>(this HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, S>> property)
            where S : IFile
        {
            return FileLineInternal<T, S>(helper, tc, property, null);
        }

        public static MvcHtmlString FileLine<T, S>(this HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, S>> property, Action<FileLine> settingsModifier)
            where S : IFile
        {
            return FileLineInternal<T, S>(helper, tc, property, settingsModifier);
        }

        public static MvcHtmlString FileLineLite<T, S>(this HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, Lite<S>>> property)
           where S : class, IFile, IEntity 
        {
            return FileLineInternal<T, Lite<S>>(helper, tc, property, null);
        }

        public static MvcHtmlString FileLineLite<T, S>(this HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, Lite<S>>> property, Action<FileLine> settingsModifier)
           where S : class, IFile, IEntity 
        {
            return FileLineInternal<T, Lite<S>>(helper, tc, property, settingsModifier);
        }

        static MvcHtmlString FileLineInternal<T, S>(HtmlHelper helper, TypeContext<T> tc, Expression<Func<T, S>> property, Action<FileLine> settingsModifier)
        {
            TypeContext<S> context = Common.WalkExpression(tc, property);

            FileLine fl = new FileLine(context.Type, context.UntypedValue, context, "", context.PropertyRoute);

            EntityBaseHelper.ConfigureEntityBase(fl, fl.Type.CleanType());

            fl.Download = FilesClient.DownloadUrlConstructors.ContainsKey(context.Type.CleanType()) ? DownloadBehaviour.View : DownloadBehaviour.None;

            Common.FireCommonTasks(fl);

            if (settingsModifier != null)
                settingsModifier(fl);

            return helper.InternalFileLine(fl);
        }

        internal static MvcHtmlString InternalFileLine(this HtmlHelper helper, FileLine fileLine)
        {
            if (!fileLine.Visible)
                return MvcHtmlString.Empty;

            IFile value = fileLine.GetFileValue();

            HtmlStringBuilder sbg = new HtmlStringBuilder();

            using (sbg.SurroundLine(new HtmlTag("div").Id(fileLine.Prefix).Class("sf-field SF-control-container")))
            {
                sbg.AddLine(new HtmlTag("link").Attrs(new { rel = "stylesheet", type = "text/css", href = RouteHelper.New().Content("~/Files/Content/Files.css") }).ToHtmlSelf());

                if (value != null)
                    sbg.AddLine(helper.Div(fileLine.Compose(EntityBaseKeys.Entity), null, "", new Dictionary<string, object> { { "style", "display:none" } }));

                fileLine.ValueHtmlProps.AddCssClass("form-control");

                bool hasEntity = value != null && value.FileName.HasText();

                using (sbg.SurroundLine(new HtmlTag("div", fileLine.Compose("DivOld")).Attr("style", "display:" + (hasEntity ? "block" : "none"))))
                {
                    HtmlStringBuilder sb = new HtmlStringBuilder();
                    using (sb.SurroundLine(new HtmlTag("div", fileLine.Compose("inputGroup")).Class("input-group")))
                    {
                        if (fileLine.Download != DownloadBehaviour.None)
                        {
                            sb.AddLine(helper.Href(fileLine.Compose(EntityBaseKeys.Link),
                                value.Try(f => f.FileName),
                                hasEntity ? FilesClient.GetDownloadUrl(value) : null,
                                value.Try(f=>f.FileName),
                                "form-control file-control",
                                fileLine.Download == DownloadBehaviour.View ? null :
                                new Dictionary<string, object> { { "download", value.Try(f => f.FileName) } }));
                        }
                        else
                        {
                            sb.AddLine(helper.Span(fileLine.Compose(EntityBaseKeys.ToStr), value.Try(f => f.FileName) ?? "", "form-control file-control", null));
                        }

                        if (fileLine.Type.IsEmbeddedEntity())
                            sb.AddLine(helper.Hidden(fileLine.Compose(EntityBaseKeys.EntityState), value.Try(f => Navigator.Manager.SerializeEntity((ModifiableEntity)f))));
                        
                        using (sb.SurroundLine(new HtmlTag("span", fileLine.Compose("shownButton")).Class("input-group-btn")))
                        {
                            sb.AddLine(EntityButtonHelper.Remove(helper, fileLine, btn: true));
{'fraction_non_alphanumeric': 0.11508852963818322, 'fraction_numerical': 0.0, 'mean_word_length': 2.193915181315304, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4929857', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2776, 'n_words': 547}