using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemX.Net.XAdaptor;
using static SystemX.Net.Platform.Common.Util.LogMessage;

namespace SystemX.Net.XAdaptor.PC
{
    public partial class XAdaptorPC
    {
        private async Task<bool> CheckFileAccess(string strFilePos)
        {
            bool bAccessResult = true;

            //File Access Check
            FileStream fs = null;

            try
            {
                //0.1초 간격으로 3회 체크 후 성공 반환
                for (int i = 0; i < 2; i++)
                {
                    fs = new FileStream(strFilePos, FileMode.Open, FileAccess.Read);

                    if (fs != null)
                    {
                        fs.Close();
                        fs = null;
                    }

                    await Task.Delay(100).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }

                bAccessResult = false;

                //File Access fail next file
                MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"Send file access check fail." + strFilePos + " [SystemX.Net.XAdaptor.PC : XPCAdaptor.WatchLogFolderScan]\r\n" + ex.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG);
            }

            return bAccessResult;
        }
        private async Task<int> DirFileSearch(string strCreateName, string strSetPath, string fileExtension, bool bCpLogFileSend)
        {
            try
            {
                string[] dirs = Directory.GetDirectories(strSetPath);
                string[] files = Directory.GetFiles(strSetPath, $"*.{fileExtension}");

                if (files.Length > 0)
                {
                    foreach (string f in files)
                    {    
                        if (await CheckFileAccess(f).ConfigureAwait(false))
                        {
                            if (FlowStreamControl.getRawQueueCnt() > 0)
                                break;

                            //FileProcess
                            if (bCpLogFileSend)
                            {
                                if (CPLogFileTransfer(f) == eFileSendRecvResult.FileProcSuccess)
                                {
                                    Random randNumber = new Random();

                                    string getFileName = Path.GetFileName(f);

                                    if (File.Exists(strCreateName + getFileName))
                                    {
                                        File.Delete(strCreateName + getFileName);
                                        File.Move(f, strCreateName + getFileName);
                                    }
                                    else
                                        File.Move(f, strCreateName + getFileName);
                                }
                            }
                            else
                            {
                                if (SendCustomFile(f) == eFileSendRecvResult.FileProcSuccess)
                                {
                                    Random randNumber = new Random();

                                    string getFileName = Path.GetFileName(f);

                                    if (File.Exists(strCreateName + getFileName))
                                    {
                                        File.Delete(strCreateName + getFileName);
                                        File.Move(f, strCreateName + getFileName);
                                    }
                                    else
                                        File.Move(f, strCreateName + getFileName);
                                }
                            }
                        }

                        //1 time 1 file slow process per 0.25 second
                        break;
                    }
                }
                /*
                if (dirs.Length > 0)
                {
                    foreach (string dir in dirs)
                    {
                        if (dir.IndexOf("Backup Folder") >= 0)
                            continue;

                        await DirFileSearch(strCreateName, dir, fileExtension, bCpLogFileSend);
                    }
                }
                */
            }
            catch (Exception ex) { }

            await Task.Delay(1).ConfigureAwait(false);

            return 0;
        }
    }
}
