<commit_before>﻿using FG5eXmlToPdf;
using FG5eXmlToPDF.Models;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;

namespace FG5eXmlToPDF
{
    public static class FG5ePdf
    {
        public static void Write(ICharacter character, string outFile)
        {
            var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("FG5eXmlToPdf.DnD_5E_CharacterSheet - Form Fillable.pdf");
            var pdfReader = new PdfReader(stream);
            var stamper = new PdfStamper(pdfReader, new FileStream(outFile, FileMode.Create));
            var form = stamper.AcroFields;

            var levels = GetLevels(character);
            form.SetField("ClassLevel", levels);

            SetProperties(character, form);
            SetAbulities(character, form);
            SetSkills(character, form);
            SetWeapons(character, form);
            SetProfLang(character, form);
            SetFeatureTraits(character, form);
            SetFeats(character, form);
            SetEquipment(character, form);
            SetDetail(character, form);
            if (character.Powers.Count > 0)
            {
                for (var level = 0; level <= character.Powers.Max(x => x.Level); level++)
                {
                    var n = 0;
                    //var take = level == 0 ? 8 : 11;
                    foreach (var spell in character.Powers.Where(x => x.Level == level))
                    {
                        form.SetField($"Spell-{level}-{n}", spell.Name);
                        form.SetField($"Prepaired_Spell-{level}-{n}", Helper.BoolToYesNo(spell.Prepaired));
                        n++;
                    }
                }
            }

            stamper.Close();
        }

        private static void SetDetail(ICharacter character, AcroFields form)
        {
            form.SetField("Text1", GenericItemListToTextBox("Features", character.Features, Environment.NewLine)
                                   + GenericItemListToTextBox("Traits", character.Traits, Environment.NewLine)
                                   + GenericItemListToTextBox("Feats", character.Feats, Environment.NewLine));
        }

        private static void SetEquipment(ICharacter character, AcroFields form)
        {
            var y = string.Empty;
            var inventory =
                character.Inventory.Aggregate(y, (current, item) => current + $"{item.Name} ({item.Text}), ");
            form.SetField("Equipment", inventory.Trim().TrimEnd(','));
        }

        private static void SetFeats(ICharacter character, AcroFields form)
        {
            var feats = GenericItemListToTitles("Feats", character.Features, Environment.NewLine);
            form.SetField("Feat+Traits", feats);
        }

        private static void SetFeatureTraits(ICharacter character, AcroFields form)
        {
            var featuresTraits = GenericItemListToTitles("Features", character.Features, Environment.NewLine) +
                                 GenericItemListToTitles("Traits", character.Traits, Environment.NewLine);
            form.SetField("Features and Traits", featuresTraits);
        }

        private static void SetProfLang(ICharacter character, AcroFields form)
        {
            var proficienciesLang = MakeTextBlock("Proficiencies", character.Proficiencies, Environment.NewLine) +
                                    MakeTextBlock("Languages", character.Languages, ", ");
            form.SetField("ProficienciesLang", proficienciesLang.Trim().TrimEnd(','));
        }

        private static void SetWeapons(ICharacter character, AcroFields form)
        {
            var n = 1;
            foreach (var weapon in character.Weapons.Take(3))<commit_msg>Fixed feats displaying on form<commit_after>﻿using FG5eXmlToPdf;
using FG5eXmlToPDF.Models;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;

namespace FG5eXmlToPDF
{
    public static class FG5ePdf
    {
        public static void Write(ICharacter character, string outFile)
        {
            var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("FG5eXmlToPdf.DnD_5E_CharacterSheet - Form Fillable.pdf");
            var pdfReader = new PdfReader(stream);
            var stamper = new PdfStamper(pdfReader, new FileStream(outFile, FileMode.Create));
            var form = stamper.AcroFields;

            var levels = GetLevels(character);
            form.SetField("ClassLevel", levels);

            SetProperties(character, form);
            SetAbulities(character, form);
            SetSkills(character, form);
            SetWeapons(character, form);
            SetProfLang(character, form);
            SetFeatureTraits(character, form);
            SetFeats(character, form);
            SetEquipment(character, form);
            SetDetail(character, form);
            if (character.Powers.Count > 0)
            {
                for (var level = 0; level <= character.Powers.Max(x => x.Level); level++)
                {
                    var n = 0;
                    //var take = level == 0 ? 8 : 11;
                    foreach (var spell in character.Powers.Where(x => x.Level == level))
                    {
                        form.SetField($"Spell-{level}-{n}", spell.Name);
                        form.SetField($"Prepaired_Spell-{level}-{n}", Helper.BoolToYesNo(spell.Prepaired));
                        n++;
                    }
                }
            }

            stamper.Close();
        }

        private static void SetDetail(ICharacter character, AcroFields form)
        {
            form.SetField("Text1", GenericItemListToTextBox("Features", character.Features, Environment.NewLine)
                                   + GenericItemListToTextBox("Traits", character.Traits, Environment.NewLine)
                                   + GenericItemListToTextBox("Feats", character.Feats, Environment.NewLine));
        }

        private static void SetEquipment(ICharacter character, AcroFields form)
        {
            var y = string.Empty;
            var inventory =
                character.Inventory.Aggregate(y, (current, item) => current + $"{item.Name} ({item.Text}), ");
            form.SetField("Equipment", inventory.Trim().TrimEnd(','));
        }

        private static void SetFeats(ICharacter character, AcroFields form)
        {
            var feats = GenericItemListToTitles("Feats", character.Feats, Environment.NewLine);
            form.SetField("Feat+Traits", feats);
        }

        private static void SetFeatureTraits(ICharacter character, AcroFields form)
        {
            var featuresTraits = GenericItemListToTitles("Features", character.Features, Environment.NewLine) +
                                 GenericItemListToTitles("Traits", character.Traits, Environment.NewLine);
            form.SetField("Features and Traits", featuresTraits);
        }

        private static void SetProfLang(ICharacter character, AcroFields form)
        {
            var proficienciesLang = MakeTextBlock("Proficiencies", character.Proficiencies, Environment.NewLine) +
                                    MakeTextBlock("Languages", character.Languages, ", ");
            form.SetField("ProficienciesLang", proficienciesLang.Trim().TrimEnd(','));
        }

        private static void SetWeapons(ICharacter character, AcroFields form)
        {
            var n = 1;
            foreach (var weapon in character.Weapons.Take(3))
{'fraction_non_alphanumeric': 0.10093652445369407, 'fraction_numerical': 0.004162330905306972, 'mean_word_length': 2.064567556795536, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3270927', 'n_tokens_mistral': 2070, 'n_tokens_neox': 1968, 'n_words': 417}