@use 'sass:map';
@use '../../styles/abstract/base' as base;
@use '../../styles/abstract/variables' as v;
@use '../../styles/abstract/functions' as f;
@use '../../styles/abstract/mixins' as m;
@use '../../styles/abstract/theme' as t;

$component: '#{base.$prefix}-time-picker';

.#{$component} {
--#{base.$prefix}-density: 0;
position: relative;
display: inline-block;

&__dialog {
box-sizing: border-box;
margin: auto;
padding: 0;
border: 0;
min-width: 280px;
max-width: 328px;
max-height: 520px;
background-color: t.color('surface-container-high');
color: t.color('on-surface');
border-radius: f.get-shape('extra-large');
@include m.elevation(3);

&[open] {
display: flex;
flex-direction: column;
animation: #{$component}-appear 0.25s cubic-bezier(0.4, 0.0, 0.2, 1);
}

&::backdrop { background: t.alpha('scrim', 0.32); }

&--horizontal {
min-width: 520px;
min-height: 360px;

.#{$component}__content {
flex-direction: row;
align-items: center;
}

.#{$component}__input-container {
flex-direction: column;
align-items: center;
}

.#{$component}__period {
flex-direction: row;
width: 216px;
height: 38px;
margin: 12px 0 0;
}

.#{$component}__period-pm {
border-block-start: 0;
border-inline-start: 1px solid t.color('outline');
}

.#{$component}__dial {
margin-inline-start: 24px;
}
}

&--24h {
.#{$component}__period {
display: none;
}
}
}

&__title {
padding: 24px 24px 16px;
@include m.typography('label-medium');
color: t.color('on-surface-variant');
}

&__content {
flex: 1;
padding: 0 24px;
overflow: hidden;
display: flex;
flex-direction: column;
align-items: center;
}

&__input-container {
display: flex;
align-items: flex-start;
justify-content: center;
margin: 0 0 24px 0;
}

&__selectors {
display: flex;
align-items: flex-start;
direction: ltr;
}

&__time-input-field {
display: flex;
flex-direction: column;
}

&__hours,
&__minutes,
&__seconds {
box-sizing: border-box;
width: 96px;
height: 80px;
margin: 0;
padding: 0;
border: 0;
border-radius: f.get-shape('small');
text-align: center;
cursor: pointer;
@include m.typography('display-large');

&[type="number"] {
height: 72px;
@include m.typography('display-medium');
background-color: t.color('surface-container-highest');
color: t.color('on-surface');
cursor: text;

&:focus {
outline: 2px solid t.color('primary');
outline-offset: -2px;
background-color: t.color('primary-container');
color: t.color('on-primary-container');
}

-moz-appearance: textfield;
&::-webkit-outer-spin-button,
&::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
}

&:not([type="number"]) {
--#{$component}-container: #{t.color('surface-container-highest')};
--#{$component}-label: #{t.color('on-surface')};
background-color: color-mix(in srgb, var(--#{$component}-label) var(--#{$component}-layer, 0%), var(--#{$component}-container));
color: var(--#{$component}-label);

&:hover { --#{$component}-layer: 8%; }
&:focus-visible, &:active { --#{$component}-layer: 10%; }

&[data-active="true"] {
--#{$component}-container: #{t.color('primary-container')};
--#{$component}-label: #{t.color('on-primary-container')};
}

&:focus { outline: none; }
&:focus-visible { outline: 3px solid t.color('secondary'); outline-offset: -3px; }
}
}

&__input-label {
margin-top: 4px;
@include m.typography('body-small');
color: t.color('on-surface-variant');
}

&__separator {
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 80px;
@include m.typography('display-large');
color: t.color('on-surface');
}

&__period {
box-sizing: border-box;
display: flex;
flex-direction: column;
flex-shrink: 0;
width: 52px;
height: 80px;
margin-inline-start: 12px;
border-radius: f.get-shape('small');
overflow: hidden;
border: 1px solid t.color('outline');

&-am,
&-pm {
display: flex;
flex: 1;
align-items: center;
justify-content: center;
cursor: pointer;
@include m.typography('title-medium');
color: t.color('on-surface-variant');

&:focus-visible {
outline: 2px solid t.color('primary');
outline-offset: -2px;
}

&:hover {
background-color: t.alpha('on-surface-variant', 0.08);
}

&:active {
background-color: t.alpha('on-surface-variant', v.state('pressed-state-layer-opacity'));
}

&[aria-disabled="true"] {
opacity: 0.38;
cursor: default;
pointer-events: none;
}
}

&-pm {
border-block-start: 1px solid t.color('outline');
}

&--selected {
background-color: t.color('tertiary-container');
color: t.color('on-tertiary-container');

&:hover {
background-color: color-mix(in srgb, t.color('on-tertiary-container') 8%, t.color('tertiary-container'));
}
}
}

&__dial {
display: flex;
align-items: center;
justify-content: center;
position: relative;
margin: 0 auto 24px;
width: 256px;
height: 256px;

}

&__dial-face {
position: relative;
width: 256px;
height: 256px;
border-radius: 50%;
background-color: t.color('surface-container-highest');
color: t.color('on-surface');
touch-action: none;
cursor: pointer;
user-select: none;
-webkit-tap-highlight-color: transparent;
transition: m.spring(--#{$component}-angle, default-spatial);
transition-property: --#{$component}-angle, --#{$component}-radius;

&--dragging { transition: none; }
&--swap[aria-label="Hour"] {
--#{$component}-digits-in: 1.2;
--#{$component}-digits-out: 0.8;
}
&--swap > .#{$component}__dial-numbers {
animation: #{$component}-digits-in-scale v.motion('spring-fast-spatial-duration') v.motion('spring-fast-spatial-easing'),
#{$component}-digits-in-fade v.motion('spring-fast-effects-duration') v.motion('spring-fast-effects-easing');
}
&--swap > .#{$component}__dial-numbers--leaving {
pointer-events: none;
animation: #{$component}-digits-out-scale v.motion('spring-fast-spatial-duration') v.motion('spring-fast-spatial-easing'),
#{$component}-digits-out-fade v.motion('spring-fast-effects-duration') v.motion('spring-fast-effects-easing') forwards;
}
}

&__dial-numbers {
position: absolute;
inset: 0;
border-radius: 50%;

&--selected {
color: t.color('on-primary');
pointer-events: none;
clip-path: circle(24px at calc(50% + sin(var(--#{$component}-angle)) * var(--#{$component}-radius)) calc(50% - cos(var(--#{$component}-angle)) * var(--#{$component}-radius)));
}
}

&__dial-number {
position: absolute;
top: 50%;
left: 50%;
box-sizing: border-box;
width: 48px;
height: 48px;
margin: -24px 0 0 -24px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
transform: translate(var(--#{$component}-x), var(--#{$component}-y));
@include m.typography('body-large');

&--inner { @include m.typography('body-medium'); }

&:focus-visible { outline: 3px solid t.color('secondary'); outline-offset: 2px; }

&[aria-disabled="true"] { opacity: 0.38; cursor: default; }
}

&__dial-track {
position: absolute;
left: calc(50% - 1px);
bottom: 50%;
width: 2px;
height: var(--#{$component}-radius);
background-color: t.color('primary');
transform-origin: 50% 100%;
transform: rotate(var(--#{$component}-angle));
pointer-events: none;
}

&__dial-centre {
position: absolute;
inset: calc(50% - 4px);
border-radius: 50%;
background-color: t.color('primary');
pointer-events: none;
}

&__dial-handle {
position: absolute;
width: 48px;
height: 48px;
left: calc(50% + sin(var(--#{$component}-angle)) * var(--#{$component}-radius) - 24px);
top: calc(50% - cos(var(--#{$component}-angle)) * var(--#{$component}-radius) - 24px);
border-radius: 50%;
background-color: t.color('primary');
pointer-events: none;

&--between::after {
content: '';
position: absolute;
inset: calc(50% - 2px);
border-radius: 50%;
background-color: t.color('on-primary');
}
}

&__actions {
display: flex;
align-items: center;
justify-content: space-between;
padding: 16px 24px 24px;
}

&__toggle-type {
font: inherit;
background: transparent;
border: none;
padding: 8px;
border-radius: 50%;
cursor: pointer;
color: t.color('on-surface-variant');

@media (max-width: 599px) {
min-height: 48px;
min-width: 48px;
}

&:focus-visible {
outline: 2px solid t.color('primary');
outline-offset: 2px;
}

&:hover {
background-color: t.alpha('on-surface', 0.08);
}

&:active {
background-color: t.alpha('on-surface', v.state('pressed-state-layer-opacity'));
}

svg {
width: 24px;
height: 24px;
display: block;
}
}

&__action-buttons {
display: flex;
gap: 8px;
}

&__cancel,
&__confirm {
padding: 10px 12px;
border: none;
background: transparent;
border-radius: f.get-shape('full');
cursor: pointer;
@include m.typography('label-large');
color: t.color('primary');

@media (max-width: 599px) {
min-height: 48px;
padding: 12px 16px;
}

&:focus-visible {
outline: 2px solid t.color('primary');
outline-offset: 2px;
}

&:hover {
background-color: t.alpha('primary', 0.08);
}

&:active {
background-color: t.alpha('primary', v.state('pressed-state-layer-opacity'));
}
}

@media (max-width: 599px) {
&__dialog {
width: 100%;
max-width: 100%;
min-height: 460px;
border-radius: 0;
margin: 0;

&--horizontal {
flex-direction: column;

.#{$component}__content {
flex-direction: column;
}

.#{$component}__dial {
margin-inline-start: 0;
}
}
}

&__input-container {
margin: 16px 0;
}

}
}

.#{$component}__dialog--input {
.#{$component}__separator { height: 72px; }
.#{$component}__period { height: 72px; }
}

.#{$component}__dialog--dial.#{$component}__dialog--24h.#{$component}__dialog--vertical .#{$component}__selectors:not(:has(.#{$component}__seconds)) :is(.#{$component}__hours, .#{$component}__minutes) {
width: 114px;
}

@keyframes #{$component}-appear {
from { opacity: 0; transform: translateY(20px) scale(0.9); }
}


@property --#{$component}-angle { syntax: '<angle>'; inherits: true; initial-value: 0deg; }
@property --#{$component}-radius { syntax: '<length>'; inherits: true; initial-value: 101px; }

@keyframes #{$component}-digits-in-scale { from { transform: scale(var(--#{$component}-digits-in, 0.8)); } to { transform: scale(1); } }
@keyframes #{$component}-digits-out-scale { from { transform: scale(1); } to { transform: scale(var(--#{$component}-digits-out, 1.2)); } }
@keyframes #{$component}-digits-in-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes #{$component}-digits-out-fade { from { opacity: 1; } to { opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
.#{$component}__dialog[open] { animation: none; }
.#{$component}__dial-face { transition: none; }
.#{$component}__dial-face--swap .#{$component}__dial-numbers { animation: none; }
}
