#if ((UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN) || (UNITY_STANDALONE && (!UNITY_IOS) && (!UNITY_ANDROID)))

using CommonUI_Unity3D.Platform;
using UnityEngine;
using CommonUI.Gemo;
using CommonUI_Win32;
using CommonLang.Concurrent;
using System.IO;
using CommonLang;
using MPQ.Updater;
using System;

namespace CommonUI_Unity3D_Win32
{
    public class UnityPlatformWin32 : IUnityPlatform
    {
        public bool IsNativeUnzip { get { return false; } }


        public void Assert(string msg)
        {
            Debug.LogError(msg);
        }

        public Texture2D SysFontTexture(
            string text, bool readable,
            CommonUI.Display.FontStyle style,
            int fontSize,
            uint fontColor,
            int borderTime,
            uint borderColor,
            CommonUI.Gemo.Size2D expectSize,
            out int boundW,
            out int boundH)
        {

            byte[] rgba = null;
            int txtw = 0;
            int txth = 0;
            Size2D size = SysFontWin32.SysFontTexture(
                text, style, fontSize, fontColor, borderTime, borderColor, expectSize,
                out rgba,
                out txtw,
                out txth);
            if (rgba != null)
            {
                Texture2D mTexture = new UnityEngine.Texture2D(
                    txtw,
                    txth,
                    TextureFormat.RGBA32,
                    false,
                    true);
                mTexture.filterMode = FilterMode.Bilinear;
                mTexture.wrapMode = TextureWrapMode.Clamp;
                mTexture.anisoLevel = 0;
                mTexture.mipMapBias = 0;
                mTexture.LoadRawTextureData(rgba);
                mTexture.Apply(false, !readable);
                boundW = txtw;
                boundH = txth;
                return mTexture;
            }
            boundW = 0;
            boundH = 0;
            return null;
        }

        public bool TestTextLineBreak(string text, float size, CommonUI.Display.FontStyle style,
            int borderTime,
            float testWidth,
            out float realWidth,
            out float realHeight)
        {
            return SysFontWin32.TestTextLineBreak(text, size, style, borderTime, testWidth, out realWidth, out realHeight);
        }


        private static void InnRect(Texture2D src, ref int sx, ref int sy)
        {
            if (sx < 0) sx = 0;
            if (sx >= src.width) sx = src.width - 1;
            if (sy < 0) sy = 0;
            if (sy >= src.height) sy = src.height - 1;
        }

        public void CopyPixels(Texture2D src, int sx, int sy, int sw, int sh, Texture2D dst, int dx, int dy)
        {
            if (sw > 0 && sh > 0)
            {
                UnityEngine.Color[] colors = src.GetPixels(sx, sy, sw, sh);
                dst.SetPixels(dx, dst.height - sh - dy, sw, sh, colors);
            }
        }


        public long GetAvaliableSpace(string path)
        {
            try
            {
                DriveInfo drive = new DriveInfo(Directory.GetDirectoryRoot(path));
                return drive.AvailableFreeSpace;
            }
            catch (Exception)
            {
                return long.MaxValue;
            }
        }

        public long GetTotalSpace(string path)
        {
            try
            {
                DriveInfo drive = new DriveInfo(Directory.GetDirectoryRoot(path));
                return drive.TotalSize;
            }
            catch (Exception)
            {
                return long.MaxValue;
            }
        }



        public bool NativeDecompressFile(MPQUpdater updater, MPQUpdater.RemoteFileInfo zip_file, MPQUpdater.RemoteFileInfo mpq_file, AtomicLong current_unzip_bytes)
        {
            return CommonMPQ.SharpZipLib.Unzip.SharpZipLib_RunUnzipMPQ(updater, zip_file, mpq_file, current_unzip_bytes);
        }
        public bool NativeDecompressMemory(ArraySegment<byte> src, ArraySegment<byte> dst)
        {
            return CommonMPQ.SharpZipLib.Unzip.SharpZipLib_DecompressZ(src, dst);
        }
        public bool NativeGetFileMD5(string fullname, out string md5)
        {
            using (var fs = new FileStream(fullname, FileMode.Open, FileAccess.Read))
            {
                md5 = CMD5.CalculateMD5(fs);
            }
            return true;
        }
    }

}
#endif