// -------------

        private void MenuItem_ImportAll(object sender, RoutedEventArgs e)
        {
            int count = 0;

            foreach (var file in FileEntries)
            {
                var id = file.GetFileIndex();
                var sr = _UODataManager.CreateSurface(System.IO.Path.Combine(_DirectoryPath, file.FileName));

                ++count;
                if ((int)tileItemView1.Tag == 0 && id < _UODataManager.ItemCapacity)
                {
                    _UODataManager.GetItemTile(id).Surface = sr;
                }
                else if ((int)tileItemView1.Tag == 1 && id < _UODataManager.LandCapacity)
                {
                    _UODataManager.GetLandTile(id).Surface = sr;
                }
                else if ((int)tileItemView1.Tag == 2 && id < _UODataManager.LandCapacity)
                {
                    _UODataManager.GetLandTile(id).Texture = sr;
                }
                else if ((int)tileItemView1.Tag == 3 && id < _UODataManager.GumpCapacity)
                {
                    _UODataManager.GetGumpSurf(id).Surface = sr;
                }
                else
                {
                    --count;
                }
            }
            System.Windows.MessageBox.Show(String.Format("Import done, loaded {0} surfaces", count));
        }
        private void lvFileList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var file = e.AddedItems[e.AddedItems.Count - 1] as FileEntry;
            var surf = UODataManager.CreateSurface(System.IO.Path.Combine(_DirectoryPath, file.FileName + file.FileExts)).GetSurface();

            imgSelectedFile.Source = surf.Image;
            imgSelectedFile.Tag    = surf;

            Comparison();
        }
        // gumps convertors       --------------------------------------------------------------

        private static unsafe void ConvertGumpSurface(UODataManager datamanager, uint extra, byte[] rawdata, out ISurface surface)
        {
            uint width = (extra >> 16) & 0xFFFF;
            uint height = extra & 0xFFFF;
            if (rawdata == null || /* extra == 0xFFFFFFFF || */ (extra & 0x80008000) != 0 || width == 0 || height == 0) {
                surface = null;
                return;
            }

            surface = datamanager.CreateSurface((ushort)width, (ushort)height, EssenceUDK.Platform.DataTypes.PixelFormat.Bpp16A1R5G5B5);
            lock (surface) {
                ushort*  line = surface.ImageWordPtr;
                uint    delta = surface.Stride >> 1;

                fixed (byte* data = rawdata) {
                    uint* lookup = (uint*)data;
                    ushort* dat = (ushort*)data;

                    for (uint count = 0, y = 0; y < height; ++y, line += delta)
                    {
                        count = (*lookup++ << 1);

                        ushort* cur = line;
                        ushort* end = line + width;

                        while (cur < end) {
                            ushort color = dat[count++];
                            ushort* next = cur + dat[count++];

                            if (color == 0)
                                cur = next;
                            else {
                                color ^= 0x8000;
                                while (cur < next)
                                    *cur++ = color;
                            }
                        }
                    }

                }
            }
        }
        // texmaps.mul convertors --------------------------------------------------------------

        private static unsafe void ConvertTexmSurface(UODataManager datamanager, uint extra, byte[] rawdata, out ISurface surface)
        {
            if (rawdata == null || rawdata.Length == 0) {
                surface = null;
                return;
            }

            // TODO: its greate loooose, we need to view at osi data model
            ushort size = (ushort)(rawdata.Length == 8192 ? 64 : 128);
            //int size = extra == 0 ? 64 : 128;

            surface = datamanager.CreateSurface(size, size, EssenceUDK.Platform.DataTypes.PixelFormat.Bpp16A1R5G5B5);
            lock (surface) {
                ushort*  line = surface.ImageWordPtr;
                uint    delta = surface.Stride >> 1;

                fixed (byte* data = rawdata)
                {
                    ushort* bindat = (ushort*)data;
                    for (int y = 0; y < size; ++y, line += delta) {
                        ushort* cur = line;
                        ushort* end = cur + size;

                        while (cur < end)
                            *cur++ = (ushort)(*bindat++ ^ 0x8000);
                    }
                }
            }
        }
        private static unsafe void ConvertItemSurface(UODataManager datamanager, byte[] rawdata, out ISurface surface)
        {
            fixed (byte* data = rawdata)
            {
                ushort* bindata = (ushort*)data;

                int count = 2;
                ushort width = bindata[count++];
                ushort height = bindata[count++];

                if (width >= 0x0400 || height >= 0x0400) {
                    surface = null;
                    return;
                }

                int[] lookups = new int[height];
                int start = (height + 4);
                for (int i = 0; i < height; ++i)
                    lookups[i] = (int)(start + (bindata[count++]));

                surface = datamanager.CreateSurface(width, height, EssenceUDK.Platform.DataTypes.PixelFormat.Bpp16A1R5G5B5);
                lock (surface) {
                    ushort*  line = surface.ImageWordPtr;
                    uint    delta = surface.Stride >> 1;

                    for (int y = 0; y < height; ++y, line += delta)
                    {
                        count = lookups[y];
                        ushort* cur = line;
                        ushort* end;
                        int xOffset, xRun;

                        while (((xOffset = bindata[count++]) + (xRun = bindata[count++])) != 0) {
                            if (cur >= surface.ImageWordPtr + delta * height)
                                break;

                            if (2 * count >= rawdata.Length)
                                break;

                            if (xOffset + xRun > delta)
                                break;

                            cur += xOffset;
                            end = cur + xRun;

                            while (cur < end)
                                *cur++ = (ushort)(bindata[count++] ^ 0x8000);
                        }
                    }
                }
            }
        }
        // art.mul convertors    ---------------------------------------------------------------

        private static unsafe void ConvertLandSurface(UODataManager datamanager, byte[] rawdata, out ISurface surface)
        {
            if (rawdata == null || rawdata.Length == 0) {
                surface = null;
                return;
            }

            fixed (byte* data = rawdata)
            {
                ushort* bdata = (ushort*)data;
                int xOffset = 21;
                int xRun = 2;

                surface = datamanager.CreateSurface(44, 44, EssenceUDK.Platform.DataTypes.PixelFormat.Bpp16A1R5G5B5);
                lock (surface) {
                    ushort*  line = surface.ImageWordPtr;
                    uint    delta = surface.Stride >> 1;

                    for (int y = 0; y < 22; ++y, --xOffset, xRun += 2, line += delta) {
                        ushort* cur = line + xOffset;
                        ushort* end = cur + xRun;

                        while (cur < end)
                            *cur++ = (ushort)(*bdata++ | 0x8000);
                    }

                    xOffset = 0;
                    xRun = 44;

                    for (int y = 0; y < 22; ++y, ++xOffset, xRun -= 2, line += delta) {
                        ushort* cur = line + xOffset;
                        ushort* end = cur + xRun;

                        while (cur < end)
                            *cur++ = (ushort)(*bdata++ | 0x8000);
                    }
                }
            }
        }
        private void Render(int cmd)
        {
            if (UOManager == null)
            {
                MessageBox.Show("Enter client path and load client mul files to be able to draw facet.");
                return;
            }

            var flt = cmd < 20;
            //if (flt)
            //    cmd -= 10;
            //else
            //    cmd -= 20;
            var _cmdlast = flt ? 10 : 20;

            if (cmdlast != _cmdlast)
            {
                avrg_ticks = avrg_draws = 0;
                cmdlast    = _cmdlast;
            }

            switch (cmd)
            {
            case 17: goto case 28;

            case 13: goto case 22;

            case 19: goto case 26;

            case 11: goto case 24;

            case 18: goto case 29;

            case 12: goto case 21;

            case 14: goto case 27;

            case 16: goto case 23;

            case 27: nudX.Value -= 1; break;

            case 23: nudX.Value += 1; break;

            case 29: nudY.Value -= 1; break;

            case 21: nudY.Value += 1; break;

            case 28: nudX.Value -= 1; nudY.Value -= 1; break;

            case 22: nudX.Value += 1; nudY.Value += 1; break;

            case 24: nudX.Value -= 1; nudY.Value += 1; break;

            case 26: nudX.Value += 1; nudY.Value -= 1; break;

            default: break;
            }

            if (nudX.Value < nudX.Minimum)
            {
                nudX.Value = nudX.Minimum;
            }
            if (nudY.Value < nudY.Minimum)
            {
                nudY.Value = nudY.Minimum;
            }
            if (nudX.Value > nudX.Maximum)
            {
                nudX.Value = nudX.Maximum;
            }
            if (nudY.Value > nudY.Maximum)
            {
                nudY.Value = nudY.Maximum;
            }

            var surw  = (ushort)nudW.Value;
            var surh  = (ushort)nudH.Value;
            var map   = (byte)nudM.Value;
            var range = (byte)nudR.Value;
            var tcx   = (ushort)nudX.Value;
            var tcy   = (ushort)nudY.Value;
            var minz  = (sbyte)nudMinZ.Value;
            var maxz  = (sbyte)nudMaxZ.Value;
            var alt   = (short)nudS.Value;



            surf = UOManager.CreateSurface(surw, surh, PixelFormat.Bpp16X1R5G5B5);
            var ticks = Environment.TickCount;

            if (flt)
            {
                UOManager.FacetRender.DrawFlatMap(map, alt, ref surf, range, tcx, tcy, minz, maxz);
            }
            else
            {
                UOManager.FacetRender.DrawObliqueMap(map, alt, ref surf, range, tcx, tcy, minz, maxz);
            }

            var take_ticks = Environment.TickCount - ticks;

            avrg_ticks += take_ticks;
            ++avrg_draws;
            lblStatus.Content = String.Format("Draw in: {0:00000} ms / Avrg: {1:00000}ms", take_ticks, avrg_ticks / avrg_draws);

            //var bid = UOManager.GetMapFacet(map).GetBlockId((uint)nudX.Value, (uint)nudY.Value);
            //UOManager.FacetRender.DrawBlock(ref surf, map, bid);
            imgFacet.Source = surf.GetSurface().Image;
        }