//
// Copyright 2018 Robert Koszewski
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

@import "mc-utils";

// ----------------------------------------------
// 3D Skin: Cape Viewer
// ----------------------------------------------

// Cape Viewer
.mc-cape-viewer(@zoom) {

  .mc-cape-viewer-@{zoom}x {
    width: 10px * @zoom;
    height: 15px * @zoom;
    margin: 10px auto;
    perspective: 800px;
    perspective-origin: 50% 100px;
    background-image: none;
	
	image-rendering:optimizeSpeed;             /* Legal fallback */
    image-rendering:-moz-crisp-edges;          /* Firefox        */
    image-rendering:-o-crisp-edges;            /* Opera          */
    image-rendering:-webkit-optimize-contrast; /* Safari         */
    image-rendering:optimize-contrast;         /* CSS3 Proposed  */
    image-rendering:crisp-edges;               /* CSS4 Proposed  */
    image-rendering:pixelated;                 /* CSS4 Proposed  */
    -ms-interpolation-mode:nearest-neighbor;   /* IE8+           */

    .wrapper {
      transform-style: preserve-3d;
      width: 10px * @zoom;
      height: 15px * @zoom;
      position: relative;
      background-image: none !important;

      .cape {
        transform-style: preserve-3d;
        //transform: rotateY(180deg) translateZ(55px) translateY(105px) rotateX(5deg);

        // Settings for All Faces
        > .front, > .back, > .left, > .right, > .top, > .bottom {
          position: absolute;
          background-size: (64px * @zoom) (32px * @zoom);
          backface-visibility: hidden;
        }

        // Sizes
        > .front, > .back {
          width: 10px * @zoom;
          height: 15px * @zoom;
        }

        > .left, > .right {
          width: 1px * @zoom;
          height: 15px * @zoom;
        }

        > .top, > .bottom {
          width: 10px * @zoom;
          height: 1px * @zoom;
        }

        // 3D Translations
        > .front {
          .tile(1, 1, 1, @zoom);
          transform: translateZ(((1px * @zoom)/2));
        }

        > .back {
          .tile(12, 1, 1, @zoom);
          transform: translateZ((((1px * @zoom)/2) * -1)) rotateY(180deg);
        }

        > .left {
          .tile(0, 1, 1, @zoom);
          transform: rotateY(270deg) translateZ((1px * @zoom)/2);
        }

        > .right {
          .tile(11, 1, 1, @zoom);
          transform: rotateY(-270deg) translateZ(((10px * @zoom) + (-1px * @zoom)/2));
        }

        > .top {
          .tile(1, 0, 1, @zoom);
          .pos3D(90deg, 0, 0, 0, @zoom / 2);
          //transform: rotateY(270deg) translateZ((1px * @zoom)/2);
        }

        > .bottom {
          .tile(11, 0, 1, @zoom);
          transform: rotateX(-90deg) translateZ(14px * @zoom + @zoom/2) ;
        }
      }
    }
  }

  // Legacy Texture Support
  .mc-cape-viewer-@{zoom}x.legacy .wrapper .cape {
    > .front, > .back, > .left, > .right {
      background-size: (22px * @zoom) (17px * @zoom);
    }
  }

  // Cape Waving Effect
  .mc-cape-viewer-@{zoom}x.waving > .wrapper > .cape {
    animation: waving 2s infinite ease-in-out;
  }

  // Cape Spinning Effect
  .mc-cape-viewer-@{zoom}x.spin > .wrapper > .cape {
    animation: spin 12s infinite linear;
  }
}

// ----------------------------------------------
// 3D Skin: Head Viewer
// ----------------------------------------------

// Cape Viewer
.mc-head-viewer(@zoom) {

  @bg_size: 64px;
  @bg_size_extended: (9/8) * 64px;

  @8px: 8px * @zoom;
  @9px: 9px * @zoom;
  @9px_offset: (@zoom /2) * -1px;
  @halfpx: 1px * @zoom / 2;

  .mc-head-viewer@{zoom}x {
    width: @8px;
    height: @8px;
    margin: 10px auto;
    perspective: 800px;
    perspective-origin: 50% 100px;
    background-image: none !important;
	
	image-rendering:optimizeSpeed;             /* Legal fallback */
	image-rendering:-moz-crisp-edges;          /* Firefox        */
	image-rendering:-o-crisp-edges;            /* Opera          */
	image-rendering:-webkit-optimize-contrast; /* Safari         */
	image-rendering:optimize-contrast;         /* CSS3 Proposed  */
	image-rendering:crisp-edges;               /* CSS4 Proposed  */
	image-rendering:pixelated;                 /* CSS4 Proposed  */
	-ms-interpolation-mode:nearest-neighbor;   /* IE8+           */

    &.spin >.head{
      animation: spin 12s infinite linear;
    }

    > .head {
      transform: rotateX(31deg) rotateY(0deg); // Bottom

      transform-style: preserve-3d;
      width: @8px;
      height: @8px;
      position: relative;
      margin: 0 auto;
      background-image: none !important;

      // Settings for All Faces
      > .front, > .back, > .left, > .right, > .top, > .bottom{
        position: absolute;
        width: @8px;
        height: @8px;
        background-size: @bg_size * @zoom;
        background-repeat: no-repeat;
        backface-visibility: hidden;
        //transform-origin: center center;
      }

      > .hat-front, > .hat-back, > .hat-left, > .hat-right, > .hat-top, > .hat-bottom{
        position: absolute;
        width: @9px; // 1 px wider
        height: @9px;
        background-size: @bg_size_extended * @zoom;
        background-repeat: no-repeat;
        backface-visibility: visible;
      }

      // 3D Translations
      > .front {
        .tile(1, 1, 8, @zoom);
        .pos3D(0, 0, 0, 0, @8px/2);
      }

      > .back {
        .tile(3, 1, 8, @zoom);
        .pos3D(0, 180deg, 0, 0, @8px/2);
      }

      > .right {
        .tile(0, 1, 8, @zoom);
        .pos3D(0, 270deg, 0, 0, @8px/2);
      }

      > .left {
        .tile(2, 1, 8, @zoom);
        .pos3D(0, 90deg, 0, 0, @8px/2);
      }

      > .top {
        .tile(1, 0, 8, @zoom);
        .pos3D(90deg, 0, 0, 0, @8px/2);
      }

      > .bottom {
        .tile(2, 0, 8, @zoom);
        .pos3D(270deg, 0, 0, 0, @8px/2);
      }

      // Head - Accessories
      > .hat-front {
        .tile(5, 1, 9, @zoom);
        .pos3D(0, 0, @9px_offset, @9px_offset, @9px/2);
      }

      > .hat-back {
        .tile(7, 1, 9, @zoom);
        .pos3D(0, 180deg, @9px_offset + @halfpx * 2, @9px_offset, @9px/2);
      }

      > .hat-right {
        .tile(4, 1, 9, @zoom);
        .pos3D(0, 270deg, @9px_offset + @halfpx, @9px_offset , @9px/2 + @halfpx);
      }

      > .hat-left {
        .tile(6, 1, 9, @zoom);
        .pos3D(0, -270deg, @9px_offset + @halfpx, @9px_offset , @9px/2 - @halfpx);
      }

      > .hat-top {
        .tile(5, 0, 9, @zoom);
        .pos3D(90deg, 0, @9px_offset, @9px_offset + @halfpx, @9px/2 + @halfpx);
      }

      > .hat-bottom {
        .tile(6, 0, 9, @zoom);
        .pos3D(-90deg, 0, @9px_offset, @9px_offset + @halfpx, @9px/2 - @halfpx);
      }
    }
  }
}

// ----------------------------------------------
// 3D Skin: Skin Viewer
// ----------------------------------------------

// Cape Viewer
.mc-skin-viewer(@zoom) {

  // Useful Variables
  @bg_size: 64px;

  @scale_9to8: 9/8;
  @offset_center_9to8: 0.5px * @zoom - 1; // TODO: Check if this is really right.

  @scale_8hto8: 8.5/8;
  @scale_8qto8: 8.25/8;
  @offset_center_8hto8: 0.5px * @zoom; // TODO: Check if this is really right.

  @halfpx: 1px * @zoom * 0.5;
  @1px: 1px * @zoom;
  @3px: 3px * @zoom;
  @4px: 4px * @zoom;
  @8px: 8px * @zoom;
  @12px: 12px * @zoom;

  // Skin Viewer
  .mc-skin-viewer-@{zoom}x {
    width: @8px;
    height: @8px + @12px + @12px;
    margin: 15px auto 40px auto;
    perspective: 800px;
    perspective-origin: 50% 100px;
    transform-style: preserve-3d;
    background-image: none !important;
	
	image-rendering:optimizeSpeed;             /* Legal fallback */
	image-rendering:-moz-crisp-edges;          /* Firefox        */
	image-rendering:-o-crisp-edges;            /* Opera          */
	image-rendering:-webkit-optimize-contrast; /* Safari         */
	image-rendering:optimize-contrast;         /* CSS3 Proposed  */
	image-rendering:crisp-edges;               /* CSS4 Proposed  */
	image-rendering:pixelated;                 /* CSS4 Proposed  */
	-ms-interpolation-mode:nearest-neighbor;   /* IE8+           */

    // Utilities
    &.hide-accessories{
      .accessory{
        display: none;
      }
    }

    &.spin >.player{
      animation: spin 12s infinite linear;
    }

    // 3D Player
    .player{
      transform-style: preserve-3d;

      // All Elements
      .front, .back, .left, .right, .top, .bottom { // Elements
        position: absolute;
        background-size: @bg_size * @zoom;
        background-repeat: no-repeat;
        backface-visibility: hidden;
        //transform-origin: center center;
      }

      .accessory{
        background-image: none;

        .front, .back, .left, .right, .top, .bottom {
          backface-visibility: visible;
        }
      }

      // #######################
      // ######## Head #########
      // #######################

      > .head {
        position: absolute;
        transform-style: preserve-3d;
        width: @8px;
        height: @8px;
        margin: 0 auto;
        background-image: none !important;

        // Settings for All Faces
        .front, .back, .left, .right, .top, .bottom{
          width: @8px;
          height: @8px;
        }

        // 3D Translations
        .front {
          .tile(1, 1, 8, @zoom);
          .pos3D(0, 0, 0, 0, @8px/2);
        }

        .back {
          .tile(3, 1, 8, @zoom);
          .pos3D(0, 180deg, 0, 0, @8px/2);
        }

        .right {
          .tile(0, 1, 8, @zoom);
          .pos3D(0, 270deg, 0, 0, @8px/2);
        }

        .left {
          .tile(2, 1, 8, @zoom);
          .pos3D(0, 90deg, 0, 0, @8px/2);
        }

        .top {
          .tile(1, 0, 8, @zoom);
          .pos3D(90deg, 0, 0, 0, @8px/2);
        }

        .bottom {
          .tile(2, 0, 8, @zoom);
          .pos3DZ(270deg, 0, 180deg, 0, 0, @8px/2);
        }

        // Head - Accessories
        > .accessory{
          transform-style: preserve-3d;
          transform: scale3d(@scale_9to8, @scale_9to8, @scale_9to8) translateY(-@offset_center_9to8);
          > .front  { .tile(5, 1, 8, @zoom); }
          > .back   { .tile(7, 1, 8, @zoom); }
          > .right  { .tile(4, 1, 8, @zoom); }
          > .left   { .tile(6, 1, 8, @zoom); }
          > .top    { .tile(5, 0, 8, @zoom); }
          > .bottom { .tile(6, 0, 8, @zoom); }
        }
      }

      // #######################
      // ######## Body #########
      // #######################
      > .body {
        position: absolute;
        transform-style: preserve-3d;
        width: @8px;
        height: @8px;
        margin: 0 auto;
        transform: translateY(@8px); // Under Head
        background-image: none !important;

        // Settings for All Faces
        .front, .back, .left, .right, .top, .bottom{
          width: @8px;
          height: @12px;
        }

        .left, .right { width: @4px; }
        .top, .bottom { height: @4px; }

        // 3D Translations
        .front {
          .tile(5, 5, 4, @zoom);
          .pos3D(0, 0, 0, 0, @4px/2);
        }

        .back {
          .tile(8, 5, 4, @zoom);
          .pos3D(0, 180deg, 0, 0, @4px/2);
        }

        .right {
          .tile(4, 5, 4, @zoom);
          .pos3D(0, 270deg, 0, 0, @4px/2);
        }

        .left {
          .tile(7, 5, 4, @zoom);
          .pos3D(0, 90deg, 0, 0, @12px/2);
        }

        .top {
          .tile(5, 4, 4, @zoom);
          .pos3D(90deg, 0, 0, 0, @4px / 2);
        }

        .bottom {
          .tile(7, 4, 4, @zoom);
          .pos3D(270deg, 0, 0, 0, @12px - (@4px /2));
        }

        // Body - Accessories (Jacket)
        > .accessory{
          .scaleAndCenter(@scale_8hto8, 12, @zoom);
          > .front  { .tile(5, 9, 4, @zoom); }
          > .back   { .tile(8, 9, 4, @zoom); }
          > .right  { .tile(4, 9, 4, @zoom); }
          > .left   { .tile(7, 9, 4, @zoom); }
          > .top    { .tile(5, 8, 4, @zoom); }
          > .bottom { .tile(7, 8, 4, @zoom); }
        }
      }

      // #######################
      // ######## Arms #########
      // #######################

      // Arms Common
      > .left-arm, > .right-arm {
        position: absolute;
        transform-style: preserve-3d;
        width: @4px;
        height: @12px;
        margin: 0 auto;
        transform: translateY(@8px) translateX(@8px / -2); // Under Head on Left Side
        background-image: none !important;

        // Settings for All Faces
        .front, .back, .left, .right, .top, .bottom{
          width: @4px;
          height: @12px;
        }

        .top, .bottom{
          height: @4px;
        }

        // 3D Translations
        .front {
          .tile(11, 5, 4, @zoom);
          .pos3D(0, 0, 0, 0, @4px/2);
        }

        .back {
          .tile(13, 5, 4, @zoom);
          .pos3D(0, 180deg, 0, 0, @4px/2);
        }

        .right {
          .tile(10, 5, 4, @zoom);
          .pos3D(0, 270deg, 0, 0, @4px/2);
        }

        .left {
          .tile(12, 5, 4, @zoom);
          .pos3D(0, 90deg, 0, 0, @4px/2);
        }

        .top {
          .tile(11, 4, 4, @zoom);
          .pos3D(90deg, 0, 0, 0, @4px / 2);
        }

        .bottom {
          .tile(12, 4, 4, @zoom);
          .pos3D(270deg, 0, 0, 0, @12px - (@4px /2));
        }

        // Body - Accessories (Jacket)
        // Right Arm - Accessories
        > .accessory{
          .scaleAndCenter(@scale_8hto8, 12, @zoom);
          .front  { .tile(11, 9, 4, @zoom); }
          .back   { .tile(13, 9, 4, @zoom); }
          .right  { .tile(10, 9, 4, @zoom); }
          .left   { .tile(12, 9, 4, @zoom); }
          .top    { .tile(11, 8, 4, @zoom); }
          .bottom { .tile(12, 8, 4, @zoom); }
        }

      }

      // Right Arm
      > .right-arm {
        transform: translateY(@8px) translateX(@8px) scaleX(-1); // Under Head on Right Side
      }

      // #######################
      // ######## Legs #########
      // #######################

      // Legs Common
      > .left-leg, > .right-leg {
        position: absolute;
        transform-style: preserve-3d;
        width: @4px;
        height: @12px;
        margin: 0 auto;
        transform: translateY(@8px + @12px); // Under Head on Left Side
        background-image: none !important;

        // Settings for All Faces
        .front, .back, .left, .right, .top, .bottom{
          width: @4px;
          height: @12px;
        }

        .top, .bottom{
          height: @4px;
        }

        // 3D Translations
        .front {
          .tile(1, 5, 4, @zoom);
          .pos3D(0, 0, 0, 0, @4px/2);
        }

        .back {
          .tile(3, 5, 4, @zoom);
          .pos3D(0, 180deg, 0, 0, @4px/2);
        }

        .right {
          .tile(0, 5, 4, @zoom);
          .pos3D(0, 270deg, 0, 0, @4px/2);
        }

        .left {
          .tile(2, 5, 4, @zoom);
          .pos3D(0, 90deg, 0, 0, @4px/2);
        }

        .top {
          .tile(1, 4, 4, @zoom);
          .pos3D(90deg, 0, 0, 0, @4px / 2);
        }

        .bottom {
          .tile(2, 4, 4, @zoom);
          .pos3D(270deg, 0, 0, 0, @12px - (@4px /2));
        }

        // Body - Accessories (Jacket)
        > .accessory{
          .scaleAndCenter(@scale_8qto8, 12, @zoom);
          .front  { .tile(1, 9, 4, @zoom); }
          .back   { .tile(3, 9, 4, @zoom); }
          .right  { .tile(0, 9, 4, @zoom); }
          .left   { .tile(2, 9, 4, @zoom); }
          .top    { .tile(1, 8, 4, @zoom); }
          .bottom { .tile(2, 8, 4, @zoom); }
        }

      }

      > .right-leg {
        transform: translateY(@8px + @12px) translateX(@4px) scaleX(-1); // Under Head on Left Side
      }
    }

    // Legacy Skin Support
    &:not(.legacy) .player {
      // Right Arm
      > .right-arm {
        transform: translateY(@8px) translateX(@8px); // Under Head on Right Side

        // Normal Faces
        .front  { .tile(9, 13, 4, @zoom); }
        .back   { .tile(11, 13, 4, @zoom); }
        .right  { .tile(8, 13, 4, @zoom); }
        .left   { .tile(10, 13, 4, @zoom); }
        .top    { .tile(9, 12, 4, @zoom); }
        .bottom { .tile(10, 12, 4, @zoom); }


        // Right Arm - Accessories
        > .accessory{
          .front  { .tile(13, 13, 4, @zoom); }
          .back   { .tile(15, 13, 4, @zoom); }
          .right  { .tile(12, 13, 4, @zoom); }
          .left   { .tile(14, 13, 4, @zoom); }
          .top    { .tile(13, 12, 4, @zoom); }
          .bottom { .tile(14, 12, 4, @zoom); }
        }
      }

      // Right Leg
      > .right-leg {
        transform: translateY(@8px + @12px) translateX(@4px); // Under Head on Left Side

        // Normal Faces
        .front  { .tile(5, 13, 4, @zoom); }
        .back   { .tile(7, 13, 4, @zoom); }
        .right  { .tile(4, 13, 4, @zoom); }
        .left   { .tile(6, 13, 4, @zoom); }
        .top    { .tile(5, 12, 4, @zoom); }
        .bottom { .tile(6, 12, 4, @zoom); }

        // Body - Accessories (Jacket)
        > .accessory{
          .front  { .tile(1, 13, 4, @zoom); }
          .back   { .tile(3, 13, 4, @zoom); }
          .right  { .tile(0, 13, 4, @zoom); }
          .left   { .tile(2, 13, 4, @zoom); }
          .top    { .tile(1, 12, 4, @zoom); }
          .bottom { .tile(2, 12, 4, @zoom); }
        }
      }
    }

    // Slim Skin Version
    &.slim .player{

      > .left-arm, > .right-arm{
        // Settings for All Faces
        .front, .back, .top, .bottom{
          width: @3px;
        }
      }

      > .left-arm {
        // Normal Faces
        .front  { .pos3D(0, 0, @zoom, 0, @4px/2); }
        .back   {
          .tile(0, 5, 4, @zoom, 51);
          .pos3D(0, 180deg, -@zoom, 0, @4px/2);
        }
        .right  { .pos3D(0, 270deg, 0, 0, @zoom); }
        .left   {
          .tile(0, 5, 4, @zoom, 47);
        }
        .top    { .pos3D(90deg, 0, @zoom, 0, @4px / 2); }
        .bottom {
          .tile(0, 4, 4, @zoom, 47);
          .pos3D(270deg, 0, @zoom, 0, @12px - (@4px /2));
        }

        // Left Arm - Accessories
        > .accessory{
          .back   { .tile(0, 9, 4, @zoom, 51); }
          .left   { .tile(0, 9, 4, @zoom, 47); }
          .bottom { .tile(0, 8, 4, @zoom, 47); }
        }
      }

      > .right-arm {
        // Normal Faces
        .back   { .tile(0, 13, 4, @zoom, 43); }
        .left   {
          .tile(0, 13, 4, @zoom, 39);
          .pos3D(0, 90deg, 0, 0, @zoom);
        }
        .bottom { .tile(0, 12, 4, @zoom, 39); }

        // Right Arm - Accessories
        > .accessory{
          .back   { .tile(0, 13, 4, @zoom, 59); }
          .left   { .tile(0, 13, 4, @zoom, 55); }
          .bottom { .tile(0, 12, 4, @zoom, 55); }
        }
      }
    }

    // Slim + Legacy
    &.slim.legacy .player{
      > .right-arm {
        transform: translateY(@8px) translateX(@8px - @zoom) scaleX(-1); // Under Head on Right Side

        // Normal Faces
        .back   { .tile(0, 5, 4, @zoom, 51); }

        > .accessory{
          .back   { .tile(0, 9, 4, @zoom, 51); } // Probably unnecessary
        }
      }
    }

    // Cloak
    .player .cape {
      transform-style: preserve-3d;
      transform: rotateY(180deg) translateX(-@zoom * 1px) translateZ(@4px / 2 + @zoom/2) translateY(@8px - @zoom/2) rotateX(5deg);

      // Settings for All Faces
      > .front, > .back, > .left, > .right, > .top, > .bottom {
        position: absolute;
        background-image: inherit!important;
        background-size: (64px * @zoom) (32px * @zoom);
        backface-visibility: hidden;
      }

      // Sizes
      > .front, > .back {
        width: 10px * @zoom;
        height: 15px * @zoom;
      }

      > .left, > .right {
        width: 1px * @zoom;
        height: 15px * @zoom;
      }

      > .top, > .bottom {
        width: 10px * @zoom;
        height: 1px * @zoom;
      }

      // 3D Translations
      > .front {
        .tile(1, 1, 1, @zoom);
        transform: translateZ(((1px * @zoom)/2));
      }

      > .back {
        .tile(12, 1, 1, @zoom);
        transform: translateZ((((1px * @zoom)/2) * -1)) rotateY(180deg);
      }

      > .left {
        .tile(0, 1, 1, @zoom);
        transform: rotateY(270deg) translateZ((1px * @zoom)/2);
      }

      > .right {
        .tile(11, 1, 1, @zoom);
        transform: rotateY(-270deg) translateZ(((10px * @zoom) + (-1px * @zoom)/2));
      }

      > .top {
        .tile(1, 0, 1, @zoom);
        .pos3D(90deg, 0, 0, 0, @zoom / 2);
      }

      > .bottom {
        .tile(11, 0, 1, @zoom);
        transform: rotateX(-90deg) translateZ(14px * @zoom + @zoom/2) ;
      }
    }

    &.legacy-cape > .player .cape {
      > .front, > .back, > .left, > .right, > .top, > .bottom {
        background-size: (22px * @zoom) (17px * @zoom);
      }
    }
  }
}

