using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace 试验版
{
    class Openfile
    {
        public class FileDataResult
        {
            public List<element> Elements { get; set; }
            public List<dian> DList { get; set; }
            public List<liness> lines { get; set; }

            public Graph graph { get; set; }
            public Graph graphll { get; set; }
        }
        
        public static FileDataResult openfiledata(string filepath, bool isremianempty = true)
        {


            if (string.IsNullOrEmpty(filepath))
                return null;
            if (!Directory.Exists(Path.GetDirectoryName(filepath)))
                Directory.CreateDirectory(Path.GetDirectoryName(filepath));
            if (!File.Exists(filepath))
            {
                return null;
            }
            List<element> oldsblist = new List<element>();
            List<dian> oldsblist2 = new List<dian>();
            List<liness> oldsblist3 = new List<liness>();

            Graph graph = new Graph();
            Graph graphll = new Graph();

            string[] sb = File.ReadAllLines(filepath);

            int[] lengths = new int[sb.Length];
            for (int i = 0; i < sb.Length; i++)
            {
                int commaCount = sb[i].Split(',').Length - 1; // 计算逗号的数量
                lengths[i] = commaCount + 1; // 逗号的个数加一
            }



            for (int j = 0; j < sb.Length; j++)
            {
                string[] sb1 = sb[j].Split(',');
                if (sb1[0] == "br")
                {
                    Bitmap bitmap = new Bitmap(200, 200);
                    Graphics g = Graphics.FromImage(bitmap);
                    Break br = new Break(g);
                    br.sbname = sb1[0];
                    br.rect2.X = float.Parse(sb1[3]);
                    br.rect2.Y = float.Parse(sb1[4]);
                    br.direction = int.Parse(sb1[7]);
                    br.rect2.Width = float.Parse(sb1[10]);
                    br.rect2.Height = float.Parse(sb1[11]);

                    br.dian1.pos.X = float.Parse(sb1[14]);
                    br.dian1.pos.Y = float.Parse(sb1[15]);
                    br.dian2.pos.X = float.Parse(sb1[16]);
                    br.dian2.pos.Y = float.Parse(sb1[17]);

                    br.dian1.line1.pos.X = float.Parse(sb1[20]);
                    br.dian1.line1.pos.Y = float.Parse(sb1[21]);
                    br.dian1.line1.pos2.X = float.Parse(sb1[22]);
                    br.dian1.line1.pos2.Y = float.Parse(sb1[23]);
                    br.dian2.line1.pos.X = float.Parse(sb1[24]);
                    br.dian2.line1.pos.Y = float.Parse(sb1[25]);
                    br.dian2.line1.pos2.X = float.Parse(sb1[26]);
                    br.dian2.line1.pos2.Y = float.Parse(sb1[27]);


                    // 读取 linkpoints
                    if (sb1[0] == "linkpoint")
                    {
                        float X = float.Parse(sb1[1]);
                        float Y = float.Parse(sb1[2]);
                        dian p = new dian(br, g);
                        p.pos.X = X;
                        p.pos.Y = Y;
                        br.dian1.linkpoints.Add(p);
                    }


                    oldsblist.Add(br);
                    oldsblist2.Add(br.dian1);
                    oldsblist2.Add(br.dian2);
                    oldsblist3.Add(br.dian1.line1);
                    oldsblist3.Add(br.dian2.line1);

                    graph.AddVertex(br.dian1);
                    graphll.AddVertex(br.dian1);
                    graph.AddVertex(br.dian2);
                    graphll.AddVertex(br.dian2);
                    graph.AddEdge(br.dian1, br.dian2);
                    graphll.AddEdge(br.dian1, br.dian2);
                    
                }
                else if (sb1[0] == "bbr")
                {
                    Bitmap bitmap = new Bitmap(200, 200);
                    Graphics g = Graphics.FromImage(bitmap);
                    dbreak bbr = new dbreak(g);
                    bbr.sbname = sb1[0];
                    bbr.rect2.X = float.Parse(sb1[3]);
                    bbr.rect2.Y = float.Parse(sb1[4]);
                    bbr.direction = int.Parse(sb1[7]);
                    bbr.rect2.Width = float.Parse(sb1[10]);
                    bbr.rect2.Height = float.Parse(sb1[11]);


                    bbr.dian1.pos.X = float.Parse(sb1[14]);
                    bbr.dian1.pos.Y = float.Parse(sb1[15]);
                    bbr.dian2.pos.X = float.Parse(sb1[16]);
                    bbr.dian2.pos.Y = float.Parse(sb1[17]);

                    bbr.dian1.line1.pos.X = float.Parse(sb1[20]);
                    bbr.dian1.line1.pos.Y = float.Parse(sb1[21]);
                    bbr.dian1.line1.pos2.X = float.Parse(sb1[22]);
                    bbr.dian1.line1.pos2.Y = float.Parse(sb1[23]);
                    bbr.dian2.line1.pos.X = float.Parse(sb1[24]);
                    bbr.dian2.line1.pos.Y = float.Parse(sb1[25]);
                    bbr.dian2.line1.pos2.X = float.Parse(sb1[26]);
                    bbr.dian2.line1.pos2.Y = float.Parse(sb1[27]);

                    oldsblist.Add(bbr);
                    oldsblist2.Add(bbr.dian1);
                    oldsblist2.Add(bbr.dian2);
                    oldsblist3.Add(bbr.dian1.line1);
                    oldsblist3.Add(bbr.dian2.line1);

                    graph.AddVertex(bbr.dian1);
                    graphll.AddVertex(bbr.dian1);
                    graph.AddVertex(bbr.dian2);
                    graphll.AddVertex(bbr.dian2);
                    graph.AddEdge(bbr.dian1, bbr.dian2);
                    graphll.AddEdge(bbr.dian1, bbr.dian2);
                }
                else if (sb1[0] == "el")
                {
                    Bitmap bitmap = new Bitmap(200, 200);
                    Graphics g = Graphics.FromImage(bitmap);
                    element el = new element(g);
                    el.sbname = sb1[0];
                    el.rect.X = float.Parse(sb1[1]);
                    el.rect.Y = float.Parse(sb1[2]);
                    el.direction = int.Parse(sb1[7]);
                    el.rect.Width = float.Parse(sb1[8]);
                    el.rect.Height = float.Parse(sb1[9]);

                    el.dian1.pos.X = float.Parse(sb1[14]);
                    el.dian1.pos.Y = float.Parse(sb1[15]);
                    

                    el.dian1.line1.pos.X = float.Parse(sb1[20]);
                    el.dian1.line1.pos.Y = float.Parse(sb1[21]);
                    el.dian1.line1.pos2.X = float.Parse(sb1[22]);
                    el.dian1.line1.pos2.Y = float.Parse(sb1[23]);
                    

                    oldsblist.Add(el);
                    oldsblist2.Add(el.dian1);
                    oldsblist3.Add(el.dian1.line1);
                    graph.AddVertex(el.dian1);
                    graphll.AddVertex(el.dian1);
                    
                    
                }
                else if (sb1[0] == "er")
                {
                    Bitmap bitmap = new Bitmap(200, 200);
                    Graphics g = Graphics.FromImage(bitmap);
                    erby er = new erby(g);
                    er.sbname = sb1[0];
                    er.rect5.X = float.Parse(sb1[5]);
                    er.rect5.Y = float.Parse(sb1[6]);
                    er.direction = int.Parse(sb1[7]);
                    er.rect5.Width = float.Parse(sb1[12]);
                    er.rect5.Height = float.Parse(sb1[13]);


                    er.dian1.pos.X = float.Parse(sb1[14]);
                    er.dian1.pos.Y = float.Parse(sb1[15]);
                    er.dian2.pos.X = float.Parse(sb1[16]);
                    er.dian2.pos.Y = float.Parse(sb1[17]);

                    er.dian1.line1.pos.X = float.Parse(sb1[20]);
                    er.dian1.line1.pos.Y = float.Parse(sb1[21]);
                    er.dian1.line1.pos2.X = float.Parse(sb1[22]);
                    er.dian1.line1.pos2.Y = float.Parse(sb1[23]);
                    er.dian2.line1.pos.X = float.Parse(sb1[24]);
                    er.dian2.line1.pos.Y = float.Parse(sb1[25]);
                    er.dian2.line1.pos2.X = float.Parse(sb1[26]);
                    er.dian2.line1.pos2.Y = float.Parse(sb1[27]);

                    oldsblist.Add(er);
                    oldsblist2.Add(er.dian1);
                    oldsblist2.Add(er.dian2);
                    oldsblist3.Add(er.dian1.line1);
                    oldsblist3.Add(er.dian2.line1);

                    graph.AddVertex(er.dian1);
                    graphll.AddVertex(er.dian1);
                    graph.AddVertex(er.dian2);
                    graphll.AddVertex(er.dian2);
                    graph.AddEdge(er.dian1, er.dian2);
                    graphll.AddEdge(er.dian1, er.dian2);

                }
                else if (sb1[0] == "tri")
                {
                    Bitmap bitmap = new Bitmap(200, 200);
                    Graphics g = Graphics.FromImage(bitmap);
                    triby tri = new triby(g);
                    tri.sbname = sb1[0];
                    tri.rect5.X = float.Parse(sb1[5]);
                    tri.rect5.Y = float.Parse(sb1[6]);
                    tri.direction = int.Parse(sb1[7]);
                    tri.rect5.Width = float.Parse(sb1[12]);
                    tri.rect5.Height = float.Parse(sb1[13]);

                    tri.dian1.pos.X = float.Parse(sb1[14]);
                    tri.dian1.pos.Y = float.Parse(sb1[15]);
                    tri.dian2.pos.X = float.Parse(sb1[16]);
                    tri.dian2.pos.Y = float.Parse(sb1[17]);
                    tri.dian3.pos.X = float.Parse(sb1[18]);
                    tri.dian3.pos.Y = float.Parse(sb1[19]);


                    tri.dian1.line1.pos.X = float.Parse(sb1[20]);
                    tri.dian1.line1.pos.Y = float.Parse(sb1[21]);
                    tri.dian1.line1.pos2.X = float.Parse(sb1[22]);
                    tri.dian1.line1.pos2.Y = float.Parse(sb1[23]);
                    tri.dian2.line1.pos.X = float.Parse(sb1[24]);
                    tri.dian2.line1.pos.Y = float.Parse(sb1[25]);
                    tri.dian2.line1.pos2.X = float.Parse(sb1[26]);
                    tri.dian2.line1.pos2.Y = float.Parse(sb1[27]);
                    tri.dian3.line1.pos.X = float.Parse(sb1[28]);
                    tri.dian3.line1.pos.Y = float.Parse(sb1[29]);
                    tri.dian3.line1.pos2.X = float.Parse(sb1[30]);
                    tri.dian3.line1.pos2.Y = float.Parse(sb1[31]);

                    oldsblist.Add(tri);
                    oldsblist2.Add(tri.dian1);
                    oldsblist2.Add(tri.dian2);
                    oldsblist2.Add(tri.dian3);
                    oldsblist3.Add(tri.dian1.line1);
                    oldsblist3.Add(tri.dian2.line1);
                    oldsblist3.Add(tri.dian3.line1);

                    graph.AddVertex(tri.dian1);
                    graphll.AddVertex(tri.dian1);
                    graph.AddVertex(tri.dian2);
                    graphll.AddVertex(tri.dian2);
                    graph.AddVertex(tri.dian3);
                    graphll.AddVertex(tri.dian3);

                    graph.AddEdge(tri.dian1, tri.dian2);
                    graph.AddEdge(tri.dian1, tri.dian3);
                    graph.AddEdge(tri.dian2, tri.dian3);

                    graphll.AddEdge(tri.dian1, tri.dian2);
                    graphll.AddEdge(tri.dian1, tri.dian3);
                    graphll.AddEdge(tri.dian2, tri.dian3);

                }
                else if(sb1[0] == "mx")
                {
                    Bitmap bitmap = new Bitmap(200, 200);
                    Graphics g = Graphics.FromImage(bitmap);
                    muxian mx = new muxian(g);
                    mx.sbname = sb1[0];
                    mx.rect2.X = float.Parse(sb1[3]);
                    mx.rect2.Y = float.Parse(sb1[4]);
                    mx.direction = int.Parse(sb1[7]);
                    mx.rect2.Width = float.Parse(sb1[10]);
                    mx.rect2.Height = float.Parse(sb1[11]);
                    
                    mx.dian1.pos.X = float.Parse(sb1[14]);
                    mx.dian1.pos.Y = float.Parse(sb1[15]);
                    mx.dian2.pos.X = float.Parse(sb1[16]);
                    mx.dian2.pos.Y = float.Parse(sb1[17]);

                    mx.dian1.line1.pos.X = float.Parse(sb1[20]);
                    mx.dian1.line1.pos.Y = float.Parse(sb1[21]);
                    mx.dian1.line1.pos2.X = float.Parse(sb1[22]);
                    mx.dian1.line1.pos2.Y = float.Parse(sb1[23]);
                    mx.dian2.line1.pos.X = float.Parse(sb1[24]);
                    mx.dian2.line1.pos.Y = float.Parse(sb1[25]);
                    mx.dian2.line1.pos2.X = float.Parse(sb1[26]);
                    mx.dian2.line1.pos2.Y = float.Parse(sb1[27]);

                    oldsblist.Add(mx);
                    oldsblist2.Add(mx.dian1);
                    oldsblist2.Add(mx.dian2);
                    oldsblist3.Add(mx.dian1.line1);
                    oldsblist3.Add(mx.dian2.line1);

                    graph.AddVertex(mx.dian1);
                    graphll.AddVertex(mx.dian1);
                    graph.AddVertex(mx.dian2);
                    graphll.AddVertex(mx.dian2);
                    graph.AddEdge(mx.dian1, mx.dian2);
                    graphll.AddEdge(mx.dian1, mx.dian2);
                }

                

            }



            for (int j = 0; j < sb.Length; j++)
            {
                
                string[] sb1 = sb[j].Split(',');

                if (sb1[0] == "el")
                {
                    for (int k = 32; k < 32 + int.Parse(sb1[lengths[j] - 3]); k++)
                    {
                        oldsblist[j].dian1.linkpoints.Add(oldsblist2[int.Parse(sb1[k])]);
                        graph.AddEdge(oldsblist[j].dian1, oldsblist2[int.Parse(sb1[k])]);
                        graphll.AddEdge(oldsblist[j].dian1, oldsblist2[int.Parse(sb1[k])]);
                    }
                }
                else if (sb1[0] == "br" || sb1[0] == "bbr" || sb1[0] == "mx" || sb1[0] == "er")
                {
                    for (int k = 32; k < 32 + int.Parse(sb1[lengths[j] - 3]); k++)
                    {
                        oldsblist[j].dian1.linkpoints.Add(oldsblist2[int.Parse(sb1[k])]);
                        graph.AddEdge(oldsblist[j].dian1, oldsblist2[int.Parse(sb1[k])]);
                        graphll.AddEdge(oldsblist[j].dian1, oldsblist2[int.Parse(sb1[k])]);
                    }

                    for (int k = 32 + int.Parse(sb1[lengths[j] - 3]); k < 32 + int.Parse(sb1[lengths[j] - 3]) + int.Parse(sb1[lengths[j] - 2]); k++)
                    {
                        oldsblist[j].dian2.linkpoints.Add(oldsblist2[int.Parse(sb1[k])]);
                        graph.AddEdge(oldsblist[j].dian2, oldsblist2[int.Parse(sb1[k])]);
                        graphll.AddEdge(oldsblist[j].dian2, oldsblist2[int.Parse(sb1[k])]);
                    }
                }
                else if (sb1[0] == "tri")
                {
                    for (int k = 32; k < 32 + int.Parse(sb1[lengths[j] - 3]); k++)
                    {
                        oldsblist[j].dian1.linkpoints.Add(oldsblist2[int.Parse(sb1[k])]);
                        graph.AddEdge(oldsblist[j].dian1, oldsblist2[int.Parse(sb1[k])]);
                        graphll.AddEdge(oldsblist[j].dian1, oldsblist2[int.Parse(sb1[k])]);
                    }

                    for (int k = 32 + int.Parse(sb1[lengths[j] - 3]); k < 32 + int.Parse(sb1[lengths[j] - 3]) + int.Parse(sb1[lengths[j] - 2]); k++)
                    {
                        oldsblist[j].dian2.linkpoints.Add(oldsblist2[int.Parse(sb1[k])]);
                        graph.AddEdge(oldsblist[j].dian2, oldsblist2[int.Parse(sb1[k])]);
                        graphll.AddEdge(oldsblist[j].dian2, oldsblist2[int.Parse(sb1[k])]);
                    }

                    for (int k = 32 + int.Parse(sb1[lengths[j] - 3]) + int.Parse(sb1[lengths[j] - 2]); k < 32 + int.Parse(sb1[lengths[j] - 3]) + int.Parse(sb1[lengths[j] - 2]) + int.Parse(sb1[lengths[j] - 1]); k++)
                    {
                        oldsblist[j].dian3.linkpoints.Add(oldsblist2[int.Parse(sb1[k])]);
                        graph.AddEdge(oldsblist[j].dian3, oldsblist2[int.Parse(sb1[k])]);
                        graphll.AddEdge(oldsblist[j].dian3, oldsblist2[int.Parse(sb1[k])]);
                    }
                }
                


                    
                    
                


            }

            return new FileDataResult
            {
                Elements = oldsblist,
                DList = oldsblist2,
                lines = oldsblist3,
                graph = graph,
                graphll = graphll
            };
            
        }




        






        

    }
}
