using FarPoint.Win.Spread.Model;
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 FCOMMON
{
    public partial class fFileExplorer : fBase
    {
        string p = string.Empty;
        public fFileExplorer(string path_)
        {
            InitializeComponent();
            this.p = path_;
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void fFileExplorer_Load(object sender, EventArgs e)
        {
            EnsureVisibleAndUsableSize();
            Application.DoEvents();
            RefreshPath();

        }
       
        void RefreshPath()
        {
            if (System.IO.Directory.Exists(this.p) == false)
            {
                Util.MsgE("지정된 폴더가 존재하지 않습니다");
                return;
            }
            else
            {
                this.listView1.Items.Clear();
                var di = new System.IO.DirectoryInfo(this.p);
                foreach (var item in di.GetFiles())
                {
                    var lv = this.listView1.Items.Add(item.Name);
                    lv.SubItems.Add(item.Extension);
                    lv.SubItems.Add(item.Length.ToString());
                    lv.SubItems.Add(item.LastWriteTime.ToString("yy-MM-dd HH:mm:ss"));
                    lv.Tag = item;
                }
            }
        }

        private void 새로고침ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RefreshPath();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //추가
            var od = new OpenFileDialog();
            if (od.ShowDialog() != DialogResult.OK) return;

            var fi = new System.IO.FileInfo(od.FileName);
            var newpath = System.IO.Path.Combine(this.p, fi.Name);
            if (System.IO.File.Exists(newpath))
            {
                if (FCOMMON.Util.MsgQ("이미 존재하는 파일명입니다. 덮어쓸까요?") != DialogResult.Yes) return;
            }
            try
            {
                System.IO.File.Copy(od.FileName, newpath, true);
                FCOMMON.Util.MsgI("파일 저장 완료\r\n파일명:" + fi.Name);
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
            RefreshPath();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            //다운로드
            if (this.listView1.FocusedItem == null) return;
            var lv = this.listView1.FocusedItem;
            var oldfile = lv.Tag as System.IO.FileInfo;//.ToString();

            var sd = new SaveFileDialog();
            sd.Filter = oldfile.Extension + "|" + oldfile.Extension.Replace(".", "*.");
            sd.FilterIndex = 0;
            sd.FileName = oldfile.Name;

            if (sd.ShowDialog() != DialogResult.OK) return;
            //var newpath = System.IO.Path.Combine(this.p, sd.FileName);
            if (System.IO.File.Exists(sd.FileName))
            {
                if (FCOMMON.Util.MsgQ("이미 존재하는 파일명입니다. 덮어쓸까요?") != DialogResult.Yes) return;
            }
            try
            {
                System.IO.File.Copy(oldfile.FullName, sd.FileName, true);
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
            RefreshPath();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            //삭제
            if (this.listView1.FocusedItem == null) return;
            var lv = this.listView1.FocusedItem;
            var newpath = lv.Tag as System.IO.FileInfo;//.ToString();

            //var newpath = System.IO.Path.Combine(this.p, od.FileName);
            if (System.IO.File.Exists(newpath.FullName) == false)
            {
                Util.MsgE("해당 파일이 존재하지 않습니다");
                return;
            }

            if (FCOMMON.Util.MsgQ("파일을 삭제할까요?") != DialogResult.Yes) return;
            try
            {
                System.IO.File.Delete(newpath.FullName);
            }
            catch (Exception ex)
            {
                FCOMMON.Util.MsgE(ex.Message);
            }
            RefreshPath();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            RefreshPath();
        }

        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }

        private void fFileExplorer_DragDrop(object sender, DragEventArgs e)
        {
            var files = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            foreach (var item in files)
            {
                var fi = new System.IO.FileInfo(item);
                var newpath = System.IO.Path.Combine(this.p, fi.Name);
                if (System.IO.File.Exists(newpath))
                {
                    if (FCOMMON.Util.MsgQ("이미 존재하는 파일명입니다.\r\n" + fi.Name + "\r\n덮어쓸까요?") != DialogResult.Yes) return;
                }
                try
                {
                    System.IO.File.Copy(item, newpath, true);
                    FCOMMON.Util.MsgI("파일 저장 완료\r\n파일명:" + fi.Name);
                }
                catch (Exception ex)
                {
                    FCOMMON.Util.MsgE(ex.Message);
                }
            }
            RefreshPath();
        }

        private void listView1_DragLeave(object sender, EventArgs e)
        {
            //this.listView1.DoDragDrop();
        }
    }
}
