import { Component, OnInit, forwardRef, OnDestroy, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReactiveFormsModule } from '@angular/forms';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
import { ScrollStrategyOptions } from '@angular/cdk/overlay';
import { OwlDateTimeModule, OwlNativeDateTimeModule } from '@danielmoncada/angular-datetime-picker';
import dayjs from 'dayjs';

import { FieldBase } from '@pega/angular-sdk-components';
import { ComponentMapperComponent } from '@pega/angular-sdk-components';
import { getDateFormatInfo } from '@pega/angular-sdk-components';
import { handleEvent } from '@pega/angular-sdk-components';
import { DateFormatters } from '@pega/angular-sdk-components';
import { PConnFieldProps } from '@pega/angular-sdk-components';

interface {{COMPONENT_CLASS_NAME}}Props extends PConnFieldProps {
  // If any, enter additional props that only exist on DateTime here
}

@Component({
  selector: 'app-{{COMPONENT_CLASS_NAME_KEBAB}}',
  templateUrl: './{{COMPONENT_CLASS_NAME_KEBAB}}.component.html',
  styleUrls: ['./{{COMPONENT_CLASS_NAME_KEBAB}}.component.scss'],
  imports: [
    CommonModule,
    ReactiveFormsModule,
    MatFormFieldModule,
    MatInputModule,
    MatDatepickerModule,
    OwlDateTimeModule,
    OwlNativeDateTimeModule,
    forwardRef(() => ComponentMapperComponent)
  ]
})
export class {{COMPONENT_CLASS_NAME}}Component extends FieldBase implements OnInit, OnDestroy {
  configProps$: {{COMPONENT_CLASS_NAME}}Props;

  stepHour = 1;
  stepMinute = 1;
  stepSecond = 1;
  public color = 'primary';
  formattedValue$: any;
  theDateFormat = getDateFormatInfo();
  timezone = PCore.getEnvironmentInfo()?.getTimeZone();
  override placeholder = `${this.theDateFormat.dateFormatStringLC}, hh:mm A`;

  private sso = inject(ScrollStrategyOptions);
  scrollStrategy = this.sso.reposition();

  override ngOnInit(): void {
    super.ngOnInit();

    if (this.formGroup$) {
      let dateTimeValue = this.value$ ?? '';

      if (this.value$) {
        dateTimeValue = dayjs(DateFormatters?.convertToTimezone(this.value$, { timezone: this.timezone }))?.toISOString();
      }
      this.fieldControl.setValue(dateTimeValue);
    }
  }

  /**
   * Updates the component when there are changes in the state.
   */
  override updateSelf(): void {
    // Resolve config properties
    this.configProps$ = this.pConn$.resolveConfigProps(this.pConn$.getConfigProps()) as {{COMPONENT_CLASS_NAME}}Props;

    // Update component common properties
    this.updateComponentCommonProperties(this.configProps$);

    // Extract the value property
    const { value } = this.configProps$;

    // Update component properties
    this.value$ = value;
    let dateTimeValue = value ?? '';
    if (this.value$) {
      dateTimeValue = dayjs(DateFormatters?.convertToTimezone(this.value$, { timezone: this.timezone }))?.toISOString();
    }
    this.fieldControl.setValue(dateTimeValue);

    if (['DISPLAY_ONLY', 'STACKED_LARGE_VAL'].includes(this.displayMode$)) {
      this.formattedValue$ = this.generateDateTime(this.value$);
    }
  }

  generateDateTime(sVal): string {
    if (!sVal) return '';
    if (sVal.length === 10) return this.utils.generateDate(sVal, 'Date-Long-Custom-YYYY');
    return this.utils.generateDateTime(sVal, 'DateTime-Long-YYYY-Custom');
  }

  fieldOnDateChange(event: any) {
    // this comes from the date pop up
    if (typeof event.value === 'object') {
      // convert date to pega "date" format
      const dateTime = dayjs(event.value?.toISOString());
      const timeZoneDateTime = (dayjs as any).tz(dateTime.format('YYYY-MM-DDTHH:mm:ss'), this.timezone);
      event.value = timeZoneDateTime && timeZoneDateTime.isValid() ? timeZoneDateTime.toISOString() : '';
    }
    handleEvent(this.actionsApi, 'changeNblur', this.propName, event.value);
  }
}
