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

namespace Project.Dialog
{
    public partial class fNewSID : Form
    {
        public int FindSIDCount = -1;
        public string NewSID { get; set; }
        public fNewSID(string presid)
        {
            InitializeComponent();
            this.tbOldSid.Text = presid;

        }

        private void fNewSID_Load(object sender, EventArgs e)
        {
            //해당 sid 로 101,103,106 가능한 sid 목록을 표시한다			
            var sid = tbOldSid.Text.Trim();
            var presid = this.tbOldSid.Text;

            var newsid = PUB.SIDCovert(presid, "fNewSID_Load", out bool converr);
            if (converr == false)
            {
                tbNewSid.Text = newsid;
            }
            else
            {
                if (newsid.Contains('|'))
                {
                    //multi sid list
                    var lst = new List<String>();
                    foreach (var SIDTo in newsid.Split('|'))
                    {
                        var msidto = presid + ";" + SIDTo;
                        lst.Add(msidto);
                    }
                    var f = new Dialog.fSelectSID(lst);
                    if (f.ShowDialog() == DialogResult.OK)
                    {
                        var v = f.Value.Split(';'); //0은 old .1=new
                        tbNewSid.Text = v[1].Trim();
                    }
                }
                else
                {
                    tbNewSid.Text = string.Empty;
                }
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.NewSID = tbNewSid.Text.Trim();

            if (NewSID.isEmpty())
            {
                UTIL.MsgE("SID value has not been entered (selected)");
                return;
            }

            if (NewSID == tbOldSid.Text)
            {
                UTIL.MsgE($"The same SID value as the existing one was confirmed\n\n" +
                    $"Existing:{tbOldSid.Text}\n" +
                   $"New:{NewSID}");
                return;
            }

            DialogResult = DialogResult.OK;
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            UTIL.TouchKeyShow(tbNewSid, "Input SID");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var old = this.tbOldSid.Text.Trim();
            var newsid = PUB.SIDCovert(old, "fNewSID_Button2Click", out bool err);

            if (err == false)
                tbNewSid.Text = newsid;
            else
            {
                tbNewSid.Text = string.Empty;
                UTIL.MsgE(newsid);
            }

        }
    }
}
