using System.Diagnostics;
using System.Security.Policy;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace DragonVideoPlayer
{
    public class ReservationItem
    {
        public string Title { get; set; }
        public string Memo { get; set; }
        public string Url { get; set; }
        public string Start { get; set; }
        public string Minutes { get; set; }
    }
    public partial class ReservationForm : Form
    {
        private DataGridView gridRecording;
        private Button btnStop;
        private Button btn_del2;
        private Button btn_clear2;

        private HashSet<string> startedSet = new HashSet<string>();
        private const string SAVE_FILE = "reservations.json";
        private MenuStrip menuStrip1;
        private TextBox txtUrl;
        private TextBox txtMemo;
        private TextBox txtTitle;
        private DateTimePicker dtStart;
        private NumericUpDown numMinutes;
        private Button btnAdd;
        private Button btnDelete;
        private Button btnClear;
        private DataGridView grid;
        private System.Windows.Forms.Timer reservationTimer;
        private readonly Dictionary<int, Process> runningProcesses = new();
        private XDocument _doc_animenosekai_epg;
        private XDocument _doc_karenda_epg;
        private XDocument _doc_jp_epg_26f0ce;
        private XDocument _doc_radiko_epg;
        public ReservationForm(XDocument doc_animenosekai_epg, XDocument doc_karenda_epg,
            XDocument doc_jp_epg_26f0ce, XDocument doc_radiko_epg)
        {
            _doc_animenosekai_epg = doc_animenosekai_epg;
            _doc_karenda_epg = doc_karenda_epg;
            _doc_jp_epg_26f0ce = doc_jp_epg_26f0ce;
            _doc_radiko_epg = doc_radiko_epg;

            InitializeUI();
            Load += ReservationForm_Load;
            FormClosing += (s, e) => SaveReservations();
        }

        private void InitializeUI()
        {
            Width = 800;
            Height = 600;
            Text = "予約録画";
            var tab = new TabControl
            {
                Dock = DockStyle.Fill
            };

            var tabMain = new TabPage("予約一覧");
            var tabRecording = new TabPage("録画中");

            tab.TabPages.Add(tabMain);
            tab.TabPages.Add(tabRecording);

            Controls.Add(tab);
            menuStrip1 = new MenuStrip();
            Controls.Add(menuStrip1);

            txtUrl = new TextBox { Left = 10, Top = 10, Width = 500 };
            txtTitle = new TextBox { Left = 10, Top = 40, Width = 600 };

            txtMemo = new TextBox { Left = 10, Top = 70, Width = 750 };
              dtStart = new DateTimePicker
            {
                Left = 20,
                Top = 105,
                Width = 150,
                Format = DateTimePickerFormat.Custom,
                CustomFormat = "yyyy/MM/dd HH:mm:ss"
            };
            numMinutes = new NumericUpDown
            {
                Left = 180,
                Top = 105,
                Width = 120,
                Minimum = 1,
                Maximum = 1440,
                Value = 60
            };

            btnAdd = new Button { Left = 320, Top = 105, Width = 70, Text = "追加" };
            btnClear = new Button { Left = 400, Top = 105, Width = 80, Text = "全クリア" };
            btnDelete = new Button { Left = 500, Top = 105, Width = 70, Text = "削除" };

            btnAdd.Click += BtnAdd_Click;
            btnDelete.Click += BtnDelete_Click;
            btnClear.Click += BtnClear_Click;

            grid = new DataGridView
            {
                Left = 20,
                Top = 140,
                Width = 550,
                Height = 330,
                AllowUserToAddRows = false,
                SelectionMode = DataGridViewSelectionMode.FullRowSelect,
                MultiSelect = false,
                ReadOnly = true
            };
            grid.ScrollBars = ScrollBars.Vertical;
            grid.Columns.Add("Title", "チャンネル名&番組名");
            grid.Columns.Add("Memo", "内容");
            grid.Columns.Add("Url", "URL");
            grid.Columns.Add("Start", "開始時間");
            grid.Columns.Add("Minutes", "録画時間(分)");

            tabMain.Controls.Add(txtUrl);
            tabMain.Controls.Add(txtTitle);
            tabMain.Controls.Add(txtMemo);
            tabMain.Controls.Add(dtStart);
            tabMain.Controls.Add(numMinutes);
            tabMain.Controls.Add(btnAdd);
            tabMain.Controls.Add(btnDelete);
            tabMain.Controls.Add(btnClear);
            tabMain.Controls.Add(grid);

            MainMenuStrip = menuStrip1;

            reservationTimer = new System.Windows.Forms.Timer();
            reservationTimer.Interval = 1000;
            reservationTimer.Tick += ReservationTimer_Tick;
            gridRecording = new DataGridView
            {
             
                Left = 20,
                Top = 40,
                Width = 400,
                Height = 500,
                AllowUserToAddRows = false,
                SelectionMode = DataGridViewSelectionMode.FullRowSelect,
                MultiSelect = false,
                ReadOnly = true
            };
            
            btnStop = new Button
            {
                
                Left = 0,
                Top = 0,
                Text = "停止",
              
                Height = 40
            };

            btn_del2 = new Button
            {

                Left = 100,
                Top = 0,
                Text = "削除",

                Height = 40
            };
            btn_clear2 = new Button
            {

                Left = 170,
                Top = 0,
                Text = "クリア",

                Height = 40
            };
            btnStop.Click += BtnStop_Click;
            btn_del2.Click += btn_del2_Click;
            btn_clear2.Click += btn_clear2_Click;

            gridRecording.ScrollBars = ScrollBars.Vertical;
            gridRecording.Columns.Add("Key", "ID");
            gridRecording.Columns.Add("Title", "タイトル");
            gridRecording.Columns.Add("Start", "開始時刻");
            tabRecording.Controls.Add(btnStop);
            tabRecording.Controls.Add(btn_del2);
            tabRecording.Controls.Add(btn_clear2);
            tabRecording.Controls.Add(gridRecording);
           
          
            
        }
        private void SaveReservations()
        {
            try
            {
                var list = new List<ReservationItem>();

                foreach (DataGridViewRow row in grid.Rows)
                {
                    list.Add(new ReservationItem
                    {
                        Title = row.Cells[0].Value?.ToString(),
                        Memo = row.Cells[1].Value?.ToString(),
                        Url = row.Cells[2].Value?.ToString(),
                        Start = row.Cells[3].Value?.ToString(),
                        Minutes = row.Cells[4].Value?.ToString()
                    });
                }

                File.WriteAllText(SAVE_FILE,
                    JsonSerializer.Serialize(list, new JsonSerializerOptions
                    {
                        WriteIndented = true
                    }));
            }
            catch { }
        }
        private void LoadReservations()
        {
            if (!File.Exists(SAVE_FILE)) return;

            try
            {
                var json = File.ReadAllText(SAVE_FILE);
                var list = JsonSerializer.Deserialize<List<ReservationItem>>(json);

                grid.Rows.Clear();

                foreach (var item in list)
                {
                    // 過去の予約はスキップ（重要）
                    if (DateTime.TryParse(item.Start, out DateTime dt))
                    {
                        if (dt < DateTime.Now) continue;
                    }

                    grid.Rows.Add(item.Title, item.Memo, item.Url, item.Start, item.Minutes);
                }
            }
            catch { }
        }
        private void ReservationForm_Load(object sender, EventArgs e)
        {
            string[] files =
            {
                "abema.txt", "abema2.txt", "mov3.txt", 
                "radiko.txt", "rakuten.txt", "fast.txt","zhongying.txt","akariko.txt"
            };

            foreach (var file in files)
            {
                AddMenuFromTxt(file);
            }

            reservationTimer.Start();
            LoadReservations();
        }

        private void AddMenuFromTxt(string file)
        {
            if (!File.Exists(file)) return;

            var root = new ToolStripMenuItem(Path.GetFileNameWithoutExtension(file));
            menuStrip1.Items.Add(root);

            foreach (var url in File.ReadAllLines(file).Where(x => !string.IsNullOrWhiteSpace(x)))
            {
                string title = GetTitleFromUrl(url);
                var item = new ToolStripMenuItem(title);
                item.Tag = url;
                item.Click += (s, e) =>
                {
                    txtUrl.Text = (string)((ToolStripMenuItem)s).Tag;
                    String desc = "";
                    if (url.Contains("radiko.jp"))
                    {
                        String title2 = title_generate.GetRadikoStation(url);
                        if (config_class.config_int_read("表示のEPG取得先") == 1)
                        { }
                        else
                        {

                          
                            var current = epg.Radik_GetCurrentProgram("radiko_epg.xml", title2, _doc_radiko_epg);
                         
                            if (current != null)
                            {
                               
                                if (string.IsNullOrEmpty(current.Name))
                                {
                                    title = title2 + " " + current.Title;

                                }
                                else
                                {
                                    title = title2 +"_"+current.Name + " " + current.Title;
                                }
                               
                               
                                desc = current.Info + current.Desc;
                              

                            }
                            else
                            {
                                title = title2;

                            }
                        }

                    }
                        if (url.Contains("linear-abematv"))
                    {
                      String title2 = title_generate.Get_title(url, title_generate._abemaMap, 3, "abema" + "abema低画質");
                        (string title, string desc, String image_path, String category) tuple_epg = default;


                        if (
                       config_class.config_int_read("表示のEPG取得先") == 3 ||
                       config_class.config_int_read("表示のEPG取得先") == 2)
                        {

                            tuple_epg = title_generate.Get_abema_bungumi_title(url, 3, "karenda_epg.xml", title_generate._Epg_naori_karenda_Map, _doc_karenda_epg);

                        }
                        if (
    config_class.config_int_read("表示のEPG取得先") == 4)
                        {

                            tuple_epg = title_generate.Get_abema_bungumi_title(url, 3, "jp_epg_26f0ce.xml", title_generate._Epg_naori_karenda_Map, _doc_karenda_epg);

                        }

                        if (config_class.config_int_read("表示のEPG取得先") == 1)
                        {
                            title = title2;

                        }
                        else
                        {
                            title =  tuple_epg.title;
                            desc = tuple_epg.desc;

                        }

                    }
                    if (url.Contains("mov3") || url.Contains("ok.ru"))
                    {
                        String title2 = title_generate.Get_mov3(url);
                        (string title, string desc, String image_path, string category) tuple_epg = default;

          
                        if (config_class.config_int_read("表示のEPG取得先") == 2)
                        {

                            tuple_epg = title_generate.Get_mov3_bungumi_title(url, "animenosekai_epg.xml", title_generate._mov3_epg_animenosekai_Map, _doc_animenosekai_epg);

                        }
                        if (config_class.config_int_read("表示のEPG取得先") == 3)
                        {

                            tuple_epg = title_generate.Get_mov3_bungumi_title(url, "karenda_epg.xml", title_generate._mov3_epg_karenda_Map, _doc_karenda_epg);

                        }


                        if (config_class.config_int_read("表示のEPG取得先") == 1)
                        {
                            title = title2;

                        }
                        else
                        {
                            title =  tuple_epg.title;
                            desc = tuple_epg.desc;

                        }
                    }



                 
                    if (url.Contains("abema.tv/now-on-air"))
                    {


                        String title2 = title_generate.Get_abema(url, 2);
                        (string title, string desc, String image_path, String category) tuple_epg = default;


                        if (
                       config_class.config_int_read("表示のEPG取得先") == 3 ||
                       config_class.config_int_read("表示のEPG取得先") == 2)
                        {

                            tuple_epg = title_generate.Get_abema_bungumi_title(url, 2, "karenda_epg.xml", title_generate._Epg_naori_karenda_Map, _doc_karenda_epg);

                        }
                        if (
       config_class.config_int_read("表示のEPG取得先") == 4)
                        {

                            tuple_epg = title_generate.Get_abema_bungumi_title(url, 2, "jp_epg_26f0ce.xml", title_generate._Epg_naori_karenda_Map, _doc_jp_epg_26f0ce);
                        }

                            if (config_class.config_int_read("表示のEPG取得先") == 1)
                        {
                            title = title2;

                        }
                        else
                        {
                            title =  tuple_epg.title;
                            desc = tuple_epg.desc;


                        }
                    }
                    if (System.Text.RegularExpressions.Regex.IsMatch(url, @"akariko\-bck1.sankuria.sbs", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                    {


                        (string title, string desc, String image_path, String category) tuple_epg = default;


                        if (config_class.config_int_read("表示のEPG取得先") == 2)
                        {

                            tuple_epg = title_generate.Get_bungumi_title(url, 3, "animenosekai_epg.xml", title_generate._Epg_naori_animenosekai_Map, _doc_animenosekai_epg);

                        }
                        if (config_class.config_int_read("表示のEPG取得先") == 3)
                        {

                            tuple_epg = title_generate.Get_bungumi_title(url, 3, "karenda_epg.xml", title_generate._Epg_naori_karenda_Map, _doc_karenda_epg);

                        }
                        if (config_class.config_int_read("表示のEPG取得先") == 4)
                        {

                            tuple_epg = title_generate.Get_bungumi_title(url, 3, "jp_epg_26f0ce.xml", title_generate._Epg_naori_karenda_Map, _doc_jp_epg_26f0ce);

                        }

                        String title2 = title_generate.Get_title(url, title_generate._akari_ChannelMap, 3, "akari") + "(akari)";

                        if (config_class.config_int_read("表示のEPG取得先") == 1)
                        {
                            title = title2;

                        }
                        else
                        {
                            title = tuple_epg.title;
                            desc = tuple_epg.desc;



                        }


                    }
                    if (System.Text.RegularExpressions.Regex.IsMatch(url, @"zhongying", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                    {
                        

                            (string title, string desc, String image_path, String category) tuple_epg = default;


                            if (config_class.config_int_read("表示のEPG取得先") == 2)
                            {

                                tuple_epg = title_generate.Get_bungumi_title(url, 3, "animenosekai_epg.xml", title_generate._Epg_naori_animenosekai_Map, _doc_animenosekai_epg);

                            }
                            if (config_class.config_int_read("表示のEPG取得先") == 3)
                            {

                                tuple_epg = title_generate.Get_bungumi_title(url, 3, "karenda_epg.xml", title_generate._Epg_naori_karenda_Map, _doc_karenda_epg);

                            }
                            if (config_class.config_int_read("表示のEPG取得先") == 4)
                            {

                                tuple_epg = title_generate.Get_bungumi_title(url, 3, "jp_epg_26f0ce.xml", title_generate._Epg_naori_karenda_Map, _doc_jp_epg_26f0ce);

                            }

                            String title2 = title_generate.Get_title(url, title_generate._naori_channelMap, 3, "zhon") + "(zhon)";

                            if (config_class.config_int_read("表示のEPG取得先") == 1)
                            {
                                title = title2;

                            }
                            else
                            {
                                title = tuple_epg.title;
                                desc = tuple_epg.desc;



                            }
                    
                       
                    }
                    if (System.Text.RegularExpressions.Regex.IsMatch(url, @"naori\-test.netgenx.site", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                    {
                       
                            (string title, string desc, String image_path, String category) tuple_epg = default;


                            if (config_class.config_int_read("表示のEPG取得先") == 2)
                            {

                                tuple_epg = title_generate.Get_bungumi_title(url, 3, "animenosekai_epg.xml", title_generate._Epg_naori_animenosekai_Map, _doc_animenosekai_epg);

                            }
                            if (config_class.config_int_read("表示のEPG取得先") == 3)
                            {

                                tuple_epg = title_generate.Get_bungumi_title(url, 3, "karenda_epg.xml", title_generate._Epg_naori_karenda_Map, _doc_karenda_epg);

                            }
                            if (config_class.config_int_read("表示のEPG取得先") == 4)
                            {

                                tuple_epg = title_generate.Get_bungumi_title(url, 3, "jp_epg_26f0ce.xml", title_generate._Epg_naori_karenda_Map, _doc_jp_epg_26f0ce);

                            }
                            String title2 = title_generate.Get_title(url, title_generate._naori_channelMap, 3, "naori") + "(naori)";

                            if (config_class.config_int_read("表示のEPG取得先") == 1)
                            {
                                title = title2;

                            }
                            else
                            {
                                title = tuple_epg.title;
                                desc = tuple_epg.desc;



                            }
                     
                       
                    }
                   

                    if (url.Contains("rakuten"))
                    {
                       
                            String title2 = title_generate.Get_title(url, title_generate._rakuten_ChannelNameMap, 2, "rakuten");
                        (string title, string desc, String image_path, String category) tuple_epg = default;

                        if (
                     config_class.config_int_read("表示のEPG取得先") == 3 ||
                     config_class.config_int_read("表示のEPG取得先") == 2)
                        {

                            tuple_epg = title_generate.Get_bungumi_title(url, 2, "karenda_epg.xml", title_generate._rakuten_epg_ChannelNameMap, _doc_karenda_epg);

                        }
                        if (config_class.config_int_read("表示のEPG取得先") == 4)
                        {

                            tuple_epg = title_generate.Get_bungumi_title(url, 2, "jp_epg_26f0ce.xml", title_generate._rakuten_epg_ChannelNameMap, _doc_jp_epg_26f0ce);

                        }

                        if (config_class.config_int_read("表示のEPG取得先") == 1)
                        {
                            title = title2;

                        }
                        else
                        {
                            title =  tuple_epg.title;
                            desc = tuple_epg.desc;



                        }
                    }

                    if (url.Contains("radiko"))
                    {
                        title = title_generate.GetRadikoStation(url);
                    }
                    if (url.Contains("fast"))
                    {
                       String title2 = title_generate.Get_title(url, title_generate._fast_channelMap, 2, "fast") + "(fast)";
                        (string title, string desc, String image_path, String category) tuple_epg = default;

                        if (config_class.config_int_read("表示のEPG取得先") == 4 ||
                     config_class.config_int_read("表示のEPG取得先") == 3 ||
                     config_class.config_int_read("表示のEPG取得先") == 2)
                        {

                            tuple_epg = title_generate.Get_bungumi_title(url, 2, "karenda_epg.xml", title_generate._fast_epg_channelMap, _doc_jp_epg_26f0ce);

                        }


                        if (config_class.config_int_read("表示のEPG取得先") == 1)
                        {
                            title = title2;

                        }
                        else
                        {
                            title =  tuple_epg.title;
                           desc = tuple_epg.desc;



                        }

                    }

                    title = filename.FileName_repalce(title);
                    txtMemo.Text =desc;
                    txtTitle.Text = ((ToolStripMenuItem)s).Text+"_" +title;
                };
                root.DropDownItems.Add(item);
            }
        }


        private string GetTitleFromUrl(string url)
        {
            if (url.Contains("linear-abematv")) return title_generate.Get_abema(url, 3);
            if (url.Contains("mov3")) return title_generate.Get_mov3(url); if (url.Contains("mov3")) return title_generate.Get_mov3(url);
            if (url.Contains("ok.ru")) return title_generate.Get_mov3(url); if (url.Contains("mov3")) return title_generate.Get_mov3(url);



            if (url.Contains("akariko")) return title_generate.Get_title(url, title_generate._akari_ChannelMap, 3, "akari");


            if (url.Contains("zhon")) return title_generate.Get_title(url, title_generate._naori_channelMap, 3, "zhon");


            if (url.Contains("naori")) return title_generate.Get_title(url, title_generate._naori_channelMap, 3, "naori");

          
            if (url.Contains("abema.tv/now-on-air")) return title_generate.Get_abema(url, 2);
            if (url.Contains("rakuten")) return title_generate.Get_title(url, title_generate._rakuten_ChannelNameMap, 2, "rakuten");
               if (url.Contains("radiko")) return title_generate.GetRadikoStation(url);
            if (url.Contains("fast")) return title_generate.Get_title(url, title_generate._fast_channelMap, 2, "fast");
            return url;
        }
              
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            grid.Rows.Add(
                txtTitle.Text,
                txtMemo.Text,
                txtUrl.Text,
                dtStart.Value.ToString("yyyy/MM/dd HH:mm:ss"),
                numMinutes.Value.ToString());
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            if (grid.SelectedRows.Count > 0)
            {
                grid.Rows.Remove(grid.SelectedRows[0]);
            }
        }

        private void BtnClear_Click(object sender, EventArgs e)
        {
            grid.Rows.Clear();
        }

        private void ReservationTimer_Tick(object sender, EventArgs e)
        {
            for (int i = grid.Rows.Count - 1; i >= 0; i--)
            {
                var row = grid.Rows[i];
                DateTime start = DateTime.Parse(row.Cells[3].Value.ToString());
                int minutes = int.Parse(row.Cells[4].Value.ToString());
                string url = row.Cells[2].Value.ToString();
                string title = row.Cells[0].Value.ToString();
                string note_text= row.Cells[1].Value.ToString();

                string uniqueId = url + start.ToString();

                if (startedSet.Contains(uniqueId))
                    continue;

                if (DateTime.Now >= start)
                {
                  
                    startedSet.Add(uniqueId);

                    grid.Rows.RemoveAt(i);

                    int key = Guid.NewGuid().GetHashCode();
                    StartRecording(url, title, note_text, minutes, key);

                    SaveReservations();
              
             
                }
            }
        }

        private void StartRecording(string url, string title,String note, int minutes, int key)
        {
            String download_folder = "";
            bool folder_bool = false;
            if (!string.IsNullOrWhiteSpace(config_class.config_text_read("録画動画を保存するフォルダのパス")) &&
                            System.IO.Directory.Exists(config_class.config_text_read("録画動画を保存するフォルダのパス")))
            {
                download_folder = config_class.config_text_read("録画動画を保存するフォルダのパス");
                folder_bool = true;
            }

            try
            {
                title = filename.FileName_repalce(title);
                string date_st = DateTime.Now.ToString("yyyy年M月d日HH時mm分");
               string output = "";
                if (string.IsNullOrEmpty(note) == true)
                {
                    if (folder_bool == true) {
                        output = $"{download_folder}/{title}_{date_st}.mp4";
                    } else
                    {
                        output = $"{title}_{date_st}.mp4";

                    }
                       
                }
                else
                {
                    note = filename.FileName_repalce(note);
                    if (folder_bool == true)
                    {
                        output = $"{download_folder}/{title}_{note}_{date_st}.mp4";
                    }
                    else
                    {
                        output = $"{title}_{note}_{date_st}.mp4";

                    }

                }
                string ytdlp_path = config_class.config_text_read("yt-dlpのパス");
                string stream_path = config_class.config_text_read("streamlinkのパス");

                ProcessStartInfo psi;

                if (Regex.IsMatch(url, @"m3u8", RegexOptions.IgnoreCase))
                {
                    psi = new ProcessStartInfo
                    {
                        FileName = ytdlp_path,
                        Arguments = $"\"{url}\" --no-part -o \"{output}\"",
                        UseShellExecute = true
                    };
                }
                else
                {
                    psi = new ProcessStartInfo
                    {
                        FileName = stream_path,
                        Arguments = $"\"{url}\" best -o \"{output}\"",
                        UseShellExecute = true
                    };
                }

                var proc = Process.Start(psi);
                runningProcesses[key] = proc;

                // 録画中リスト追加（UIスレッド）
                BeginInvoke(() =>
                {
                    gridRecording.Rows.Add(key, title, DateTime.Now.ToString("HH:mm:ss"));
                });

                // 自動終了
                Task.Run(async () =>
                {
                    await Task.Delay(TimeSpan.FromMinutes(minutes));

                    StopRecording(key);
                });
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
        private void btn_del2_Click(object sender, EventArgs e)
        {
            if (gridRecording.SelectedRows.Count > 0)
            {
                gridRecording.Rows.Remove(gridRecording.SelectedRows[0]);
            }
        }
        private void btn_clear2_Click(object sender, EventArgs e)
        {
            gridRecording.Rows.Clear();
        }

      
        private void StopRecording(int key)
        {
            try
            {
                if (runningProcesses.TryGetValue(key, out var p))
                {
                    if (!p.HasExited)
                        p.Kill(true);
                }
            }
            catch { }

            runningProcesses.Remove(key);

            // UIから削除
            BeginInvoke(() =>
            {
                foreach (DataGridViewRow row in gridRecording.Rows)
                {
                    if ((int)row.Cells[0].Value == key)
                    {
                        gridRecording.Rows.Remove(row);
                        break;
                    }
                }
            });
        }
        private void BtnStop_Click(object sender, EventArgs e)
        {
            if (gridRecording.SelectedRows.Count == 0) return;

            int key = (int)gridRecording.SelectedRows[0].Cells[0].Value;

            StopRecording(key);
        }
    }
}
