import { DatePipe, NgFor, NgIf } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { GjDatetimeDirective } from '@directives/gj-datetime.directive';
import { Roots } from '@enums/root.enum';
import { UserInformationForm } from '@interfaces/form';
import { IonicModule, NavController } from '@ionic/angular';
import { StateManagementService } from '@services/state-management.service';
import { UtilityService } from '@services/utility.service';
import { format, parseISO } from 'date-fns';

@Component({
  selector: 'app-passive-user-information',
  templateUrl: './passive-user-information.page.html',
  styleUrls: ['./passive-user-information.page.scss'],
  standalone: true,
  imports: [IonicModule, FormsModule, ReactiveFormsModule, NgIf, NgFor, GjDatetimeDirective, DatePipe]
})
export class PassiveUserInformationPage implements OnInit {

  userInformationForm: FormGroup<UserInformationForm>;
  minDate: string;
  relationshipsDetails = [
    {
      id: 1,
      name: 'Son',
    },
    {
      id: 2,
      name: 'Daughter',
    }
  ];
  currentDate = this.formatDate((new Date()).toISOString());
  childBirthDateValue = '';
  constructor(
    private navCtrl: NavController,
    private stateManagementService: StateManagementService,
    private router: Router,
    public utilityService: UtilityService
  ) { }

  ngOnInit() {
    if (this.router.getCurrentNavigation().extras.state) {
      const childData = this.router.getCurrentNavigation().extras.state.childData;
      this.stateManagementService.userInformation.id = childData.id;
      this.stateManagementService.userInformation.name = childData.name;
      this.stateManagementService.userInformation.relationship = childData.relationship;
      this.stateManagementService.userInformation.birth_date = childData.birth_date;
      this.stateManagementService.userInformation.school_name = childData.school_name;
      this.stateManagementService.userInformation.aboutUser = childData.about;
      this.stateManagementService.userInformation.avatar = childData.avatar;
    }
    this.userInformationForm = new FormGroup<UserInformationForm>({
      fullName: new FormControl(this.stateManagementService.userInformation.name, Validators.required),
      relationship: new FormControl(this.stateManagementService.userInformation.relationship, Validators.required),
      birthDate: new FormControl(this.stateManagementService.userInformation.birth_date, Validators.required),
    });
    this.childBirthDateValue = this.stateManagementService.userInformation.birth_date;
    this.minDate = this.formatDate(new Date((new Date()).setFullYear((new Date()).getFullYear() - 14)).toISOString());
  }

  formatDate(value) {
    return format(parseISO(value), 'yyyy-MM-dd');
  }

  moveToNext(form) {
    if (!form.valid) {
      this.userInformationForm.markAllAsTouched();
      return;
    }
    if (form.value.birthDate) {
      this.userInformationForm.controls.birthDate.setValue(this.formatDate(form.value.birthDate));
      form.value.birthDate = this.formatDate(form.value.birthDate);
    }
    this.stateManagementService.userInformation.name = form.value.fullName;
    this.stateManagementService.userInformation.birth_date = this.formatDate(form.value.birthDate);
    this.stateManagementService.userInformation.relationship = form.value.relationship;
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.passiveUserEducation);
  }
}
