using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1.Mac
{
    public partial class Mac_Input : Form
    {
        Main main;
        public Mac_Input(Main _main)
        {
            InitializeComponent();
            main = _main;
        }
        private void Mac_Input_Load(object sender, EventArgs e)
        {
            string[] com_list = { "표준Marc", "라인Marc" };
            comboBox1.Items.AddRange(com_list);
            comboBox1.SelectedIndex = 0;

        }
        private void btn_FileOpen_Click(object sender, EventArgs e)
        {
            string file_path = string.Empty;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                file_path = openFileDialog1.FileName;
                try
                {
                    StreamReader r = new StreamReader(file_path, Encoding.Default);
                    input_Grid(r.ReadToEnd());

                    r.Close();
                }
                catch(Exception ex)
                {
                    // 
                    MessageBox.Show(ex.ToString());
                }
            }
            tb_filePath.Text = file_path;
        }
        void input_Grid(string text)
        {
            string[] grid = text.Split('');
            for (int a = 0; a < grid.Length - 1; a++)
            {
                string[] data = Split_Marc(grid[a]);
                data[5] = grid[a] + "";
                dataGridView1.Rows.Add(data);
            }
        }
        string[] Split_Marc(string text)
        {
            string tmp_string = text.Remove(0, 24);
            string[] Marc = tmp_string.Split('');
            string[] dir = made_Directory(Marc[0]);
            string[] data = { "", "", "", "", "", "" };     // isbn, 도서명, 저자, 출판사, 정가
            String_Text st = new String_Text();

            for(int a = 0; a < dir.Length; a++)
            {
                if (dir[a] == "020")
                {
                    data[0] = st.GetMiddelString(Marc[a + 1], "a", "");   // isbn
                    data[4] = st.GetMiddelString(Marc[a + 1], "c", "");   // 정가
                }
                if (dir[a] == "245")
                {
                    data[1] = st.GetMiddelString(Marc[a + 1], "a", "");      // 도서명
                    data[2] = st.GetMiddelString(Marc[a + 1], "d", "");      // 저자
                }
                if (dir[a] == "260")
                {
                    data[3] = st.GetMiddelString(Marc[a + 1], "b", "");    // 출판사
                }
                if (dir[a] == "950")
                {
                    data[4] = st.GetMiddelString(Marc[a + 1], "b", "");
                }
            }
            return data;
        }
        string[] made_Directory(string value)
        {
            int length = value.Length;
            int start = 0;
            List<string> L_dir = new List<string>();
            while (true)
            {
                if (start + 12 > length) { break; }
                L_dir.Add(value.Substring(start, 3));
                start += 12;
            }
            string[] result = L_dir.ToArray();
            
            return result;
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Skill_Grid sg = new Skill_Grid();
            sg.Print_Grid_Num(sender, e);
        }
    }
}
