using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ConvertFileToJson
{
    class Program
    {
        static void Main(string[] args)
        {
            string strPathFile = string.Empty,
                   strBuffer = string.Empty,
                   strJson = string.Empty;

            List<Header> lst = null;

            strPathFile = ConfigurationManager.AppSettings["PathFile"].ToString();

            strBuffer = File.ReadAllText(strPathFile, Encoding.Default);

            lst = GetListCp(strBuffer);

            foreach(Header obj in lst)
            {
                strJson += JsonConvert.SerializeObject(obj);
            }

        }


        static List<Header> GetListCp(string strBuffer)
        {
            List<Header> lstCp = new List<Header>();

            foreach (string strLine in strBuffer.Split(new[] { "\r\n" }, StringSplitOptions.None))
            {
                string[] aryFields = strLine.Split(new[] { "|" }, StringSplitOptions.None);

                Header cp = new Header();

                for (int i = 0; i < aryFields.Count(); i++)
                {
                    switch (i)
                    {
                        case 0:
                            if (!String.IsNullOrEmpty(aryFields[i]))
                            {
                                cp.d_codigo = aryFields[i];
                            }
                            break;
                        case 1:
                            cp.d_asenta = aryFields[i];
                            break;
                        case 2:
                            cp.d_tipo_asenta = aryFields[i];
                            break;
                        case 3:
                            cp.D_mnpio= aryFields[i];
                            break;
                        case 4:
                            cp.d_estado = aryFields[i];
                            break;
                        case 5:
                            cp.d_ciudad = aryFields[i];
                            break;
                        case 6:
                            if (!String.IsNullOrEmpty(aryFields[i]))
                            {
                                cp.d_CP = aryFields[i];
                            }
                            break;
                        case 7:
                            cp.c_estado = aryFields[i];
                            break;
                        case 8:
                            cp.c_oficina = aryFields[i];
                            break;
                        case 9:
                            if (!String.IsNullOrEmpty(aryFields[i]))
                            {
                                cp.c_CP = aryFields[i];
                            }
                            break;
                        case 10:
                            if (!String.IsNullOrEmpty(aryFields[i]))
                            {
                                cp.c_tipo_asenta = aryFields[i];
                            }
                            break;
                        case 11:
                            if (!String.IsNullOrEmpty(aryFields[i]))
                            {
                                cp.c_mnpio = aryFields[i];
                            }
                            break;
                        case 12:
                            if (!String.IsNullOrEmpty(aryFields[i]))
                            {
                                cp.id_asenta_cpcons = aryFields[i];
                            }
                            break;
                        case 13:
                            cp.d_zona = aryFields[i];
                            break;
                        case 14:
                            if (!String.IsNullOrEmpty(aryFields[i]))
                            {
                                cp.c_cve_ciudad = aryFields[i];
                            }
                            break;
                    }
                }

                lstCp.Add(cp);
            }
            return lstCp;
        }
    }

}
