//190806    chi getWorkWeek 추가
//190805    chi MakeCSVString 추가
//180903    chi makefilepath/ ftppath 추가
//180807    chi rad2deg, deg2rad 추가
//180625    chi  ToCharHexString,ToStringFromHexString 추가
//180624    chi isLocalApplication 추가
//180618    chi GetCSVBuffer 추가
//180614    chi map 명령어 추가
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace FCOMMON
{
    public static partial class Util
    {

        /// <summary>
        /// 공유폴더를 탐색기로 표시합니다
        /// </summary>
        /// <param name="subdir"></param>
        /// <param name="pathlist"></param>
        public static void OpenSharedPath(List<string> subdir, params string[] pathlist)
        {
            var serverpath = FCOMMON.DBM.getCodeSvalue("55", "01");
            //var serverpath = FCOMMON.info.datapath;
            if (System.IO.Directory.Exists(serverpath) == false)
            {
                FCOMMON.Util.MsgE(
                    "프로젝트 기본경로가 존재하지 않아 진행할 수 없습니다\n\n" +
                    "공용정보->공용코드->55-01 데이터를 설정 하시기 바랍니다" +
                    serverpath);
                return;
            }

            foreach (var dir in pathlist)
                serverpath = System.IO.Path.Combine(serverpath, dir);


            if (System.IO.Directory.Exists(serverpath) == false)
            {
                try
                {
                    System.IO.Directory.CreateDirectory(serverpath);

                    if(subdir !=null)
                    foreach (var dir in subdir)
                        System.IO.Directory.CreateDirectory(serverpath + "\\" + dir);

                }
                catch (Exception eX)
                {
                    FCOMMON.Util.MsgE("프로젝트 저장소 생성실패\n" + eX.Message);
                    return;
                }
            }

            FCOMMON.Util.RunExplorer(serverpath);
        }



        public static Boolean MakeDateString(string src, out string data)
        {
            data = src;
            string dtStr = string.Empty;
            DateTime dt;
            int iv;
            if (int.TryParse(src, out iv))
            {
                if (iv <= 31)
                {
                    src = DateTime.Now.ToString("yyyy-MM-") + iv.ToString("00");
                }
                else if (src.Length <= 2)
                {
                    //숫자이긴하나 32보다크면 오류로 한다.
                    return false;
                }
            }
            src = src.Replace("/", "-");
            if (src.Length < 4)
            {
                src = src.PadLeft(4, '0');
            }
            if (src.Length == 4)
            {
                src = DateTime.Now.ToString("yyyy") + "-" + src.Substring(0, 2) + "-" + src.Substring(2);
            }
            if (src.Length == 6)
            {
                src = "20" + src.Substring(0, 2) + "-" + src.Substring(2, 2) + "-" + src.Substring(4, 2);
            }
            if (src.Length == 5 && src.Substring(2, 1) == "-")
            {
                src = DateTime.Now.ToString("yyyy-") + src;
            }

            if (DateTime.TryParse(src, out dt))
            {
                data = dt.ToString("yyyy-MM-dd");
                return true;
            }
            else data = src;
            return false;
        }
        public static void SetFormStatus(ref System.Windows.Forms.Form f, string formid, Boolean read)
        {
            var fi = new System.IO.FileInfo(info.Path + "formSetting\\" + formid + ".xml");
            if (fi.Directory.Exists == false) fi.Directory.Create();
            arUtil.XMLHelper xml = new arUtil.XMLHelper(fi.FullName);
            if (!xml.Exist())
            {
                xml.CreateFile();
                if (read) return; //읽기인데 파일이 없으므로 넘어간다.
            }
            if (read)
            {
                var scr = Screen.AllScreens;
                int maxWidth = scr[0].Bounds.Width;
                int maxHeigh = scr[0].Bounds.Height;
                if (scr.Length > 1)
                {
                    foreach (var disp in scr)
                    {
                        if (disp.Bounds.X + disp.Bounds.Width > maxWidth)
                            maxWidth = disp.Bounds.X + disp.Bounds.Width;
                        if (disp.Bounds.Y + disp.Bounds.Height > maxHeigh)
                            maxHeigh = disp.Bounds.Y + disp.Bounds.Height;
                    }
                }
                var leftStr = xml.get_Data("position", "left");
                var topStr = xml.get_Data("position", "top");
                int l = 0;
                int t = 0;
                if (!int.TryParse(leftStr, out l)) l = 0;
                if (!int.TryParse(topStr, out t)) t = 0;
                if (f.StartPosition != FormStartPosition.CenterScreen && (l != 0 || t != 0))
                {
                    if (l > (maxWidth - 10)) l = 0;
                    if (t >= (maxHeigh - 10)) t = 0;

                    f.Location = new System.Drawing.Point(l, t);
                }

                if (f.FormBorderStyle == FormBorderStyle.Sizable ||
                    f.FormBorderStyle == FormBorderStyle.SizableToolWindow)
                {
                    var wStr = xml.get_Data("size", "width");
                    var hStr = xml.get_Data("size", "height");
                    int w = 0;
                    int h = 0;
                    if (!int.TryParse(wStr, out w)) w = 0;
                    if (!int.TryParse(hStr, out h)) h = 0;
                    if (w != 0 || h != 0)
                    {
                        f.Size = new System.Drawing.Size(w, h);
                    }
                }

            }
            else
            {
                xml.set_Data("position", "left", f.Left.ToString());
                xml.set_Data("position", "top", f.Top.ToString());

                if (f.FormBorderStyle == FormBorderStyle.Sizable ||
                   f.FormBorderStyle == FormBorderStyle.SizableToolWindow)
                {
                    xml.set_Data("size", "width", f.Width.ToString());
                    xml.set_Data("size", "height", f.Height.ToString());
                }
                xml.Save();
            }

        }
    }
}
