using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FlightRoute;

namespace FlyExe
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public bool isSuccess;//记录绕行最终是否成功
        public MainWindow()
        {
            InitializeComponent();
        }
        //保存输出文本
        private static void SaveFile(string filePath, string txtCon)
        {
            txtCon = txtCon.TrimEnd((char[])"\n\r".ToCharArray());//去除最后的回车符
            Console.WriteLine(txtCon);
            string stream = null;
            if (File.Exists(filePath))
            {
                StreamReader reader = new StreamReader(filePath);
                stream = reader.ReadToEnd();
                reader.Close();
            }
            FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(txtCon);
            sw.Close();
            fs.Close();
        }
        //回调 输出日志
        public void StrPrint(string str)
        {
            vLogBox.Text += "\r\n"+str;
        }
        //资源管理器 获取文件路径及文件名
        private string OpExplorer(string titName, out bool isSelect)
        {
            Microsoft.Win32.OpenFileDialog ofdl = new Microsoft.Win32.OpenFileDialog();//打开资源管理器
            ofdl.Filter = "航点文件|*.txt;*.svg";//资源管理器限制扩展名
            ofdl.Title = titName;
            if (ofdl.ShowDialog() == true)//执行打开资源管理器 并判断有没有获取到文件名
            {
                isSelect = true;
            }
            else
            {
                isSelect = false;
            }
            return ofdl.FileName;//返回路径及文件名
        }

        //3D绕行 按钮执行
        private void Button_Click_ABypassB(object sender, RoutedEventArgs e)
        {
            //导入航点
            bool isSelect;//判断有没有获取到文件名
            string fileName = OpExplorer("导入航点文件", out isSelect);
            StrPrint("开始");
            if (isSelect)//如果读取到文件路径名称
            {
                List<Vector3[]> txt = FlyBase.TxtToPos(fileName, out string[] fightNames);//从txt文件里面读取航点 信息
                //3D绕行计算
                List<Vector3[]> txtC = FlyBase.ExeABypassB(txt[0], txt[1], out this.isSuccess, StrPrint);
                string txta = "";
                string txtb = "";
                string txtc = "";
                int id = 1;
                foreach (Vector3 item in txtC[0])
                {
                    txta += id + " 0" + " " + item.x + " " + item.y + " " + item.z + "\r\n";
                    if (txtC.Count > 1) txtb += id + " 0" + " " + txtC[1][id - 1].x + " " + txtC[1][id - 1].y + " " + txtC[1][id - 1].z + "\r\n";
                    if (txtC.Count > 2) txtc += id + " 0" + " " + txtC[2][id - 1].x + " " + txtC[2][id - 1].y + " " + txtC[2][id - 1].z + "\r\n";
                    id++;
                }
                SaveFile("C:/Users/szdot/Desktop/a.txt", txta);
                if (txtC.Count > 1) SaveFile("C:/Users/szdot/Desktop/b.txt", txtb);
                if (txtC.Count > 2) SaveFile("C:/Users/szdot/Desktop/c.txt", txtc);
            }
            if (this.isSuccess == true)
            {
                StrPrint("成功");
            }
            else
            {
                StrPrint("未成功");
            }
            StrPrint("结束");
        }

        //碰撞次数排序
        private void Button_Click_CollisionSort(object sender, RoutedEventArgs e)
        {
            //导入航点
            bool isSelect;//判断有没有获取到文件名
            string fileName = OpExplorer("导入航点文件", out isSelect);
            if (isSelect)//如果读取到文件路径名称
            {
                List<Vector3[]> txt = FlyBase.TxtToPos(fileName, out string[] fightNames);//从txt文件里面读取航点 信息
                //碰撞检测
                List<int[]> airCollision = FlyBase.AirImitation(txt[0],txt[1]);
                string txta="";
                foreach (int[] item in airCollision)
                {
                    txta += item[0]+"-"+item[1]+",";
                }
                SaveFile("C:/Users/ddkk/Desktop/a.txt", txta);
            }
        }
    }
}
