import { css } from 'lit';

export const styles = css`
  :host {
    position: relative;
    display: block;
    --state-inactive-color: var(--state-icon-color);
    --button-card-ripple-icon-inset-padding: 12px;
  }

  :host(.section) {
    display: unset;
    position: static;
  }

  ha-card {
    cursor: pointer;
    overflow: hidden;
    box-sizing: border-box;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    line-height: normal;

    --ha-ripple-color: var(--button-card-ripple-color);
    --ha-ripple-hover-color: var(--ha-ripple-color, var(--button-card-ripple-hover-color));
    --ha-ripple-pressed-color: var(--ha-ripple-color, var(--button-card-ripple-pressed-color));
    --ha-ripple-hover-opacity: var(--button-card-ripple-hover-opacity, 0.04);
    --ha-ripple-pressed-opacity: var(--button-card-ripple-pressed-opacity, 0.12);

    -webkit-touch-callout: none; /* iOS Safari */
    -webkit-user-select: none; /* Safari */
    -khtml-user-select: none; /* Konqueror HTML */
    -moz-user-select: none; /* Old versions of Firefox */
    -ms-user-select: none; /* Internet Explorer/Edge */
    user-select: none; /* Non-prefixed version, currently
                          supported by Chrome, Opera and Firefox */
  }
  ha-card.disabled {
    pointer-events: none;
    cursor: default;
  }
  ha-card.section {
    height: 100%;
  }
  ha-ripple[icon] {
    inset: var(--button-card-ripple-icon-inset, var(--dynamic-ripple-icon-inset, 5px));
    border-radius: var(--button-card-ripple-icon-border-radius, var(--ha-card-border-radius, 12px));
    --md-ripple-color: var(--button-card-ripple-icon-color, var(--ha-ripple-color));
    --md-ripple-hover-color: var(--button-card-ripple-icon-color, var(--ha-ripple-hover-color));
    --md-ripple-pressed-color: var(--button-card-ripple-pressed-color, var(--ha-ripple-pressed-color));
    --md-ripple-hover-opacity: var(
      --button-card-ripple-icon-hover-opacity,
      calc(var(--ha-ripple-hover-opacity, 0.04) + 0.05)
    );
    --md-ripple-pressed-opacity: var(
      --button-card-ripple-icon-pressed-opacity,
      calc(var(--ha-ripple-pressed-opacity, 0.12) + 0.05)
    );
  }
  wa-tooltip {
    --wa-tooltip-background-color: var(--button-card-tooltip-background-color, var(--secondary-background-color));
    --wa-tooltip-content-color: var(--button-card-tooltip-content-color, var(--primary-text-color));
    --wa-tooltip-font-family: var(
      --button-card-tooltip-font-family,
      var(--ha-tooltip-font-family, var(--ha-font-family-body))
    );
    --wa-tooltip-font-size: var(--button-card-tooltip-font-size, var(--ha-tooltip-font-size, var(--ha-font-size-s)));
    --wa-tooltip-font-weight: var(
      --button-card-tooltip-font-weight,
      var(--ha-tooltip-font-weight, var(--ha-font-weight-normal))
    );
    --wa-tooltip-line-height: var(
      --button-card-tooltip-line-height,
      var(--ha-tooltip-line-height, var(--ha-line-height-condensed))
    );
    --wa-tooltip-padding: var(--button-card-tooltip-padding, 8px);
    --wa-tooltip-border-radius: var(
      --button-card-tooltip-border-radius,
      var(--ha-tooltip-border-radius, var(--ha-border-radius-sm))
    );
    --wa-tooltip-arrow-size: var(--button-card-tooltip-arrow-size, var(--ha-tooltip-arrow-size, 8px));
    --wa-tooltip-border-width: var(--button-card-tooltip-border-width);
    --wa-tooltip-border-color: var(--button-card-tooltip-border-color);
    --wa-tooltip-border-style: var(--button-card-tooltip-border-style);
    --max-width: var(--button-card-tooltip-max-width, 30ch);
  }
  wa-tooltip::part(base__popup) {
    --show-duration: var(--button-card-tooltip-show-duration, 100ms);
    --hide-duration: var(--button-card-tooltip-hide-duration, 100ms);
    opacity: var(--button-card-tooltip-opacity, 1);
  }
  wa-tooltip::part(body) {
    padding: var(--button-card-tooltip-padding, 0.25em 0.5em);
    box-shadow: var(--button-card-tooltip-box-shadow, var(--ha-card-box-shadow, none));
    font-weight: var(--button-card-tooltip-font-weight, var(--ha-tooltip-font-weight, normal));
    font-family: var(--button-card-tooltip-font-family, var(--ha-tooltip-font-family, inherit));
    text-align: var(--button-card-tooltip-text-align, center);
    text-decoration: var(--button-card-tooltip-text-decoration, none);
    text-transform: var(--button-card-tooltip-text-transform, none);
    overflow-wrap: var(--button-card-tooltip-overflow-wrap, normal);
  }
  :not(ha-state-icon) ha-icon,
  ha-state-icon {
    display: inline-block;
    margin: auto;
    --mdc-icon-size: 100%;
    --iron-icon-width: 100%;
    --iron-icon-height: 100%;
  }
  ha-card.button-card-main {
    padding: 4% 0px;
    text-transform: none;
    font-weight: 400;
    font-size: 1.2rem;
    align-items: center;
    text-align: center;
    letter-spacing: normal;
    width: 100%;
  }
  .ellipsis {
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
  }
  #spinner {
    align-items: flex-start;
    justify-content: flex-start;
    padding: 8px 7px;
    --ha-spinner-indicator-color: var(--button-card-spinner-color, var(--button-card-color));
    --ha-spinner-size: var(--button-card-spinner-size, 1.5vw);
    /* DO NOT override items below */
    pointer-events: auto;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    display: flex;
  }
  #spinner-background {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    opacity: var(--button-card-spinner-background-opacity, 0.4);
    background: var(--button-card-spinner-background-color, var(--card-background-color, white));
  }

  #overlay {
    align-items: flex-start;
    justify-content: flex-end;
    padding: 8px 7px;
    opacity: 0.5;
    /* DO NOT override items below */
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    display: flex;
  }
  #lock {
    -webkit-animation-fill-mode: both;
    animation-fill-mode: both;
    margin: unset;
    width: 24px;
  }
  .invalid {
    animation: blink 1s cubic-bezier(0.68, -0.55, 0.27, 1.55) infinite;
  }
  .hidden {
    visibility: hidden;
    opacity: 0;
    transition:
      visibility 0s 1s,
      opacity 1s linear;
  }
  @keyframes blink {
    0% {
      opacity: 1;
    }
    50% {
      opacity: 0;
    }
    100% {
      opacity: 1;
    }
  }
  @-webkit-keyframes rotating /* Safari and Chrome */ {
    from {
      -webkit-transform: rotate(0deg);
      -o-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    to {
      -webkit-transform: rotate(360deg);
      -o-transform: rotate(360deg);
      transform: rotate(360deg);
    }
  }
  @keyframes rotating {
    from {
      -ms-transform: rotate(0deg);
      -moz-transform: rotate(0deg);
      -webkit-transform: rotate(0deg);
      -o-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    to {
      -ms-transform: rotate(360deg);
      -moz-transform: rotate(360deg);
      -webkit-transform: rotate(360deg);
      -o-transform: rotate(360deg);
      transform: rotate(360deg);
    }
  }
  [rotating] {
    -webkit-animation: rotating 2s linear infinite;
    -moz-animation: rotating 2s linear infinite;
    -ms-animation: rotating 2s linear infinite;
    -o-animation: rotating 2s linear infinite;
    animation: rotating 2s linear infinite;
  }

  #container {
    display: grid;
    width: 100%;
    height: 100%;
    text-align: center;
    align-items: center;
  }
  .section #container {
    align-content: center;
  }
  #img-cell {
    display: flex;
    grid-area: i;
    height: 100%;
    width: 100%;
    max-width: 100%;
    max-height: 100%;
    align-self: center;
    justify-self: center;
    overflow: hidden;
    justify-content: center;
    align-items: center;
    position: relative;
  }

  ha-state-icon#icon {
    height: 100%;
    width: 100%;
    max-height: 100%;
    position: absolute;
  }
  ha-state-icon#icon.enabled,
  img#icon.enabled {
    pointer-events: auto;
    cursor: pointer;
  }
  img#icon {
    display: block;
    height: auto;
    width: 100%;
    position: absolute;
  }
  #name {
    grid-area: n;
    max-width: 100%;
    align-self: center;
    justify-self: center;
    /* margin: auto; */
  }
  #state {
    grid-area: s;
    max-width: 100%;
    align-self: center;
    justify-self: center;
    /* margin: auto; */
  }

  #label {
    grid-area: l;
    max-width: 100%;
    align-self: center;
    justify-self: center;
  }

  #container.vertical {
    grid-template-areas: 'i' 'n' 's' 'l';
    grid-template-columns: 1fr;
    grid-template-rows: 1fr min-content min-content min-content;
  }
  .section #container.vertical {
    grid-template-rows: auto min-content min-content min-content;
  }
  .section #container.vertical:not(.no-icon) #name {
    margin-top: 8px;
  }
  /* Vertical No Icon */
  #container.vertical.no-icon {
    grid-template-areas: 'n' 's' 'l';
    grid-template-columns: 1fr;
    grid-template-rows: 1fr min-content 1fr;
  }
  #container.vertical.no-icon #state {
    align-self: center;
  }
  #container.vertical.no-icon #name {
    align-self: end;
  }
  #container.vertical.no-icon #label {
    align-self: start;
  }

  /* Vertical No Icon No Name */
  #container.vertical.no-icon.no-name {
    grid-template-areas: 's' 'l';
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }
  #container.vertical.no-icon.no-name #state {
    align-self: end;
  }
  #container.vertical.no-icon.no-name #label {
    align-self: start;
  }

  /* Vertical No Icon No State */
  #container.vertical.no-icon.no-state {
    grid-template-areas: 'n' 'l';
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }
  #container.vertical.no-icon.no-state #name {
    align-self: end;
  }
  #container.vertical.no-icon.no-state #label {
    align-self: start;
  }

  /* Vertical No Icon No Label */
  #container.vertical.no-icon.no-label {
    grid-template-areas: 'n' 's';
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }
  #container.vertical.no-icon.no-label #name {
    align-self: end;
  }
  #container.vertical.no-icon.no-label #state {
    align-self: start;
  }

  /* Vertical No Icon No Label No Name */
  #container.vertical.no-icon.no-label.no-name {
    grid-template-areas: 's';
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }
  #container.vertical.no-icon.no-label.no-name #state {
    align-self: center;
  }
  /* Vertical No Icon No Label No State */
  #container.vertical.no-icon.no-label.no-state {
    grid-template-areas: 'n';
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }
  #container.vertical.no-icon.no-label.no-state #name {
    align-self: center;
  }

  /* Vertical No Icon No Name No State */
  #container.vertical.no-icon.no-name.no-state {
    grid-template-areas: 'l';
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }
  #container.vertical.no-icon.no-name.no-state #label {
    align-self: center;
  }

  #container.icon_name_state {
    grid-template-areas: 'i n' 'l l';
    grid-template-columns: 40% 1fr;
    grid-template-rows: 1fr min-content;
  }

  #container.icon_name {
    grid-template-areas: 'i n' 's s' 'l l';
    grid-template-columns: 40% 1fr;
    grid-template-rows: 1fr min-content min-content;
  }
  .section #container.icon_name {
    grid-template-rows: auto min-content min-content;
  }
  #container.icon_state {
    grid-template-areas: 'i s' 'n n' 'l l';
    grid-template-columns: 40% 1fr;
    grid-template-rows: 1fr min-content min-content;
  }
  .section #container.icon_state {
    grid-template-rows: auto min-content min-content;
  }

  #container.name_state {
    grid-template-areas: 'i' 'n' 'l';
    grid-template-columns: 1fr;
    grid-template-rows: 1fr min-content min-content;
  }
  .section #container.name_state {
    grid-template-rows: auto min-content min-content;
  }
  #container.name_state.no-icon {
    grid-template-areas: 'n' 'l';
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }
  #container.name_state.no-icon #name {
    align-self: end;
  }
  .section #container.name_state:not(.no-icon) #name {
    margin-top: 8px;
  }
  #container.name_state.no-icon #label {
    align-self: start;
  }

  #container.name_state.no-icon.no-label {
    grid-template-areas: 'n';
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }
  #container.name_state.no-icon.no-label #name {
    align-self: center;
  }

  /* icon_name_state2nd default */
  #container.icon_name_state2nd {
    grid-template-areas: 'i n' 'i s' 'i l';
    grid-template-columns: 40% 1fr;
    grid-template-rows: 1fr min-content 1fr;
  }
  #container.icon_name_state2nd #name {
    align-self: end;
  }
  #container.icon_name_state2nd #state {
    align-self: center;
  }
  #container.icon_name_state2nd #label {
    align-self: start;
  }

  /* icon_name_state2nd No Label */
  #container.icon_name_state2nd.no-label {
    grid-template-areas: 'i n' 'i s';
    grid-template-columns: 40% 1fr;
    grid-template-rows: 1fr 1fr;
  }
  #container.icon_name_state2nd #name {
    align-self: end;
  }
  #container.icon_name_state2nd #state {
    align-self: start;
  }

  /* icon_state_name2nd Default */
  #container.icon_state_name2nd {
    grid-template-areas: 'i s' 'i n' 'i l';
    grid-template-columns: 40% 1fr;
    grid-template-rows: 1fr min-content 1fr;
  }
  #container.icon_state_name2nd #state {
    align-self: end;
  }
  #container.icon_state_name2nd #name {
    align-self: center;
  }
  #container.icon_state_name2nd #label {
    align-self: start;
  }

  /* icon_state_name2nd No Label */
  #container.icon_state_name2nd.no-label {
    grid-template-areas: 'i s' 'i n';
    grid-template-columns: 40% 1fr;
    grid-template-rows: 1fr 1fr;
  }
  #container.icon_state_name2nd #state {
    align-self: end;
  }
  #container.icon_state_name2nd #name {
    align-self: start;
  }

  #container.icon_label {
    grid-template-areas: 'i l' 'n n' 's s';
    grid-template-columns: 40% 1fr;
    grid-template-rows: 1fr min-content min-content;
  }

  [style*='--aspect-ratio'] > :first-child {
    width: 100%;
  }
  [style*='--aspect-ratio'] > img {
    height: auto;
  }
  @supports (--custom: property) {
    [style*='--aspect-ratio'] {
      position: relative;
    }
    [style*='--aspect-ratio']::before {
      content: '';
      display: block;
      padding-bottom: calc(100% / (var(--aspect-ratio)));
    }
    [style*='--aspect-ratio'] > :first-child {
      position: absolute;
      top: 0;
      left: 0;
      height: 100%;
    }
  }
`;

export default styles;
