using System;
using System.Runtime.Serialization;
using System.IO;
using Com.Bocom.OpenApi.Security.Digest;
using Com.Bocom.OpenApi.Utils;

namespace Com.Bocom.OpenApi
{
    namespace Response
    {
        [Serializable]
        [DataContract]
        public class FileDownloadResponseV1 : BocomResponse
        {
            private string filePath;

            [DataMember]
            public string file_name { get; set; }

            [DataMember]
            public string file_hashcode { get; set; }

            public string GetFilePath()
            {
                return filePath;
            }

            public void SetFilePath(string filePath)
            {
                this.filePath = filePath;
            }


            public bool isSuccess()
            {
                if (!IsSuccess())
                {
                    return false;
                }

                if (!filePath.EndsWith("/") || !filePath.EndsWith("\\"))
                {
                    filePath = filePath + "/";
                }

                string tempFileName = file_name + ".TMP";
                FileItem file = new FileItem(filePath + tempFileName);
                try
                {
                    string fileHashcode = SHA256Digest.Digest(filePath + this.file_name);

                    if (!fileHashcode.Equals(file_hashcode))
                    {
                        File.Delete(filePath + file_name);

                        throw new Exception("Bocom fileHashCode digest not passed.");
                    }

                    File.Delete(filePath + tempFileName);

                    return true;
                }
                catch (Exception e)
                {
                    throw new Exception("文件重命名失败", e);
                }
            }
        }
    }
}