using System;
using System.Collections.Generic;
using System.Text;

namespace DxBlazorApplication2.AS400Kommunikation
{
    /// <summary>
    /// Klasse zur Speicherung von Feldinformationen.
    /// Anhand dieser Informationen wird später der String der eigentlichen
    /// Datensätze zerlegt!
    /// </summary>
    public class AS400SocketField
    {
        private String name = null;
        private String type = null;
        private int start = 0;
        private int length = 0;
        private int internalDecimalPlaces = 0;
        private String description = null;
        private string satzart = "";

        public AS400SocketField(String line)
        {
            this.extractField(line);
        }

        public AS400SocketField(string satzArt, String name, String typ, int start, int länge, int nk, string beschreibung)
        {
            this.name = name;
            this.type = typ;
            this.start = start;
            this.length = länge;
            this.internalDecimalPlaces = nk;
            this.description = beschreibung;
            this.satzart = satzArt;
        }

        public AS400SocketField()
        {
        }

        public string Satzart
        {
            get
            {
                return satzart;
            }
            set
            {
                satzart = value;
            }
        }

        public String Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public String Type
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
            }
        }

        public int Start
        {
            get
            {
                return start;
            }
            set
            {
                start = value;
            }
        }

        public int Length
        {
            get
            {
                return length;
            }
            set
            {
                length = value;
            }
        }


        public int InternalDecimalPlaces
        {
            get
            {
                return internalDecimalPlaces;
            }
            set
            {
                internalDecimalPlaces = value;
            }
        }

        public String Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }

        private void extractField(string line)
        {
            line.PadRight(23);
            ///Zerlegen des Strings in die Felder
            this.name = line.Substring(2, 8).Trim();
            this.description = line.Substring(23).Trim();
            this.start = Convert.ToInt32(line.Substring(13, 5));
            this.length = Convert.ToInt32(line.Substring(18, 4));
            this.type = line.Substring(12, 1).Trim();
            string nachKomma = line.Substring(22, 1);
            if (!nachKomma.Equals(" "))
            {
                this.internalDecimalPlaces = Convert.ToInt32(nachKomma);
            }
            else
            {
                this.internalDecimalPlaces = 0;
            }
        }

        public String constructLine()
        {
            StringBuilder result = new StringBuilder();
            ///Zusammenbauen des Strings aus den Feldern
            //Satzart
            result.Append(satzart.PadRight(2));
            //Feldname
            result.Append(name.PadRight(10));
            //Typ
            result.Append(type);
            //5 Stellen Start
            result.Append(start.ToString("00000"));
            //4 Stellen Länge
            result.Append(length.ToString("0000"));
            //Nachkommastellen wenn vorhanden
            if (type.Equals("S") || type.Equals("P"))
            {
                result.Append(internalDecimalPlaces.ToString("0"));
            }
            else
            {
                result.Append(" ");
            }
            //Text
            result.Append(description);

            return result.ToString();
        }

    }
}
