using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using skyscraper2.TsDuckInterface;
using skyscraper2.TsDuckInterface.Inputs;
using skyscraper2.TsDuckInterface.Outputs;
using skyscraper2.TsDuckInterface.Processors;
using skyscraper2.TsDuckInterface.Tsscan;

namespace skyscraper5.Skyscraper
{
    class DvbTMagic
    {
        private readonly XmlSerializer xs;

        public DvbTMagic()
        {
            xs = new XmlSerializer(typeof(tsduck));
        }

        public void Run()
        {
            if (File.Exists("uhf.xml"))
                File.Delete("uhf.xml");
            Process tsscan = new Process();
            tsscan.StartInfo.FileName = "tsscan";
            tsscan.StartInfo.Arguments = "--debug=8 -a 1 --delivery-system=DVB-T2 --uhf-band --save-channels uhf.xml";
            tsscan.Start();
            tsscan.WaitForExit();
            if (File.Exists("uhf.xml"))
            {
                tsduck uhfOut = (tsduck)xs.Deserialize(new StringReader(File.ReadAllText("uhf.xml")));
                BatchScan(uhfOut);
            }

            if (File.Exists("vhf.xml"))
                File.Delete("vhf.xml");
            tsscan.StartInfo.Arguments = "--debug=8 -a 1 --delivery-system=DVB-T2 --vhf-band --save-channels vhf.xml";
            tsscan.Start();
            tsscan.WaitForExit();
            if (File.Exists("vhf.xml"))
            {
                tsduck uhfOut = (tsduck)xs.Deserialize(new StringReader(File.ReadAllText("vhf.xml")));
                BatchScan(uhfOut);
            }
        }

        private void BatchScan(tsduck tsscan)
        {
            if (tsscan.Items == null)
                return;
            if (tsscan.Items.Length == 0)
                return;

            foreach (tsduckNetwork network in tsscan.Items)
            {
                foreach (tsduckNetworkTS ts in network.ts)
                {
                    foreach (tsduckNetworkTSDvbt dvbt in ts.dvbt)
                    {
                        string freqString = dvbt.frequency.Replace(",", "");
                        long freq = Int64.Parse(freqString);

                        TspCommandBuilder cmdBuilder = new TspCommandBuilder();
                        cmdBuilder.Verbose = true;

                        DvbInput dvbInput = new DvbInput();
                        dvbInput.AdapterId = 1;
                        dvbInput.Frequency = freq;
                        dvbInput.DeliverySystem = DeliverySystem.DVB_T2;
                        cmdBuilder.Input = dvbInput;
                        
                        cmdBuilder.Processors.Add(new HistoryProcessor());

                        UntilProcessor untilProcessor = new UntilProcessor();
                        untilProcessor.Bytes = freq;
                        cmdBuilder.Processors.Add(untilProcessor);

                        long mhz = freq / 1000 / 1000;
                        cmdBuilder.Output = new FileOutput(new FileInfo(String.Format("{0}.ts", mhz)));

                        cmdBuilder.ExecuteAndWait();
                    }
                }
            }
        }
    }
}
