using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using hirdParty.DownloadSystem;
using ThirdParty.DownloadSystem;
using UnityEngine;

public class CheckFileThran
{
    private Thread _thread;
    private string rootPath;

    public List<MD5FileInfo> shiBaiFile = new List<MD5FileInfo>();
    public int count;
    public bool isEnd;
    public CheckFilePool CheckFilePool;
    public System.Action OnClick;

    public void Start(string dPath, CheckFilePool CheckFilePool)
    {
        this.CheckFilePool = CheckFilePool;
        this.rootPath = dPath;
        _thread = new Thread(UpdateThread);
        _thread.Start();
        shiBaiFile.Clear();
    }

    public void Close()
    {
        _thread = null;
        OnClick = null;
    }

    private void UpdateThread()
    {
        while (_thread != null)
        {
            // for (int i = 0; i < md5FileInfos.Count; i++)
            {
                if (_thread == null)
                {
                    break;
                }


                try
                {
                    MD5FileInfo bdMd5File = null;
                    lock (CheckFilePool.CheckFile)
                    {
                        if (CheckFilePool.CheckFile.Count > 0)
                        {
                            bdMd5File = CheckFilePool.CheckFile[0];
                            CheckFilePool.CheckFile.Remove(bdMd5File);
                        }
                    }

                    if (bdMd5File == null)
                    {
                        break;
                    }

                    if (bdMd5File.size <= 0)
                    {
                        continue;
                    }

                    if (bdMd5File.fileName.Contains("MD5"))
                    {
                        continue;
                    }

                    count++;

                    string dPath = rootPath + bdMd5File.fileName;
                    if (!File.Exists(dPath))
                    {
                        // Debug.Log("文件不纯在"+ dPath);
                        shiBaiFile.Add(bdMd5File);
                    }
                    else
                    {
                        string md5 = FileMD5(dPath);

                        if (!bdMd5File.md5.Equals(md5))
                        {
                            Debug.Log("MD5校验失败" + dPath);

                            shiBaiFile.Add(bdMd5File);
                        }
                    }

                    Thread.Sleep(1);
                }
                catch (Exception e)
                {
                    Debug.LogError("MD5校验失败" + e);
                }
            }
        }


        isEnd = true;
        OnClick?.Invoke();
    }

    public static string FileMD5(string filePath)
    {
        byte[] retVal;
        using (FileStream file = new FileStream(filePath, FileMode.Open))
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            retVal = md5.ComputeHash(file);
        }

        return ByteArrayToString(retVal);
    }

    public static string ByteArrayToString(byte[] ba)
    {
        StringBuilder hex = new StringBuilder(ba.Length * 2);
        foreach (byte b in ba)
        {
            hex.AppendFormat("{0:x2}", b);
        }

        return hex.ToString();
    }

    public string GetMD5HashFromFile(byte[] data)
    {
        try
        {
            // FileStream file = new FileStream(filename,FileMode.Open);
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] retVal = md5.ComputeHash(data);
            // StringBuilder sb = new StringBuilder();
            StringBuilder hex = new StringBuilder(retVal.Length * 2);
            foreach (byte b in retVal)
            {
                hex.AppendFormat("{0:x2}", b);
            }

            return retVal.ToString();
        }
        catch (System.Exception ex)
        {
            Debug.Log("错误信息：" + ex);
            return null;
        }
    }
}