import { Injectable } from '@angular/core';
import { FormControl, FormGroup, UntypedFormBuilder, Validators } from '@angular/forms';
import { reviewedFields } from '@interfaces/common';
import {
  CertificateForm,
  CertificateTermConditionForm,
  DikshaInformationForm,
  SanghAddressForm, SanghDonationForm, SanghFacilitiesForm, SanghInformationForm, SanghMemberForm, SanghProfileImageForm, SansarikJeevanInformationForm,
  SantInformationForm, SantProfileImageForm
} from '@interfaces/form';
import { aadharValidator, accountNumberValidator } from '../validators/common.validator';

@Injectable({
  providedIn: 'root'
})
export class SantInfoService {

  santInformationForm: FormGroup;
  sanghInformationForm: FormGroup;
  certificateInformationForm: FormGroup;
  sanghMemberForm: FormGroup;
  updateSant = false;
  updateSantId: string;
  reviewed_fields: reviewedFields | null;

  constructor(
    private formBuilder: UntypedFormBuilder
  ) {
    this.santInformationForm = this.formBuilder.group({
      santInfoForm: this.formBuilder.group({
        name: new FormControl('', Validators.required),
        honor: new FormControl(''),
        dharma_id: new FormControl('', Validators.required),
        sampraday_id: new FormControl('', Validators.required),
        birth_date: new FormControl(''),
        gender: new FormControl('', Validators.required)
      }),
      dikshaInfoForm: this.formBuilder.group({
        diksha_date: new FormControl(''),
        diksha_place: new FormControl(''),
        diksha_place_latitude: new FormControl(''),
        diksha_place_longitude: new FormControl(''),
        guru: new FormControl(''),
        guru_id: new FormControl('')
      }),
      sansarikJeevanInfoForm: this.formBuilder.group({
        father_name: new FormControl(''),
        father_id: new FormControl(''),
        father_type: new FormControl(''),
        mother_name: new FormControl(''),
        mother_id: new FormControl(''),
        mother_type: new FormControl(''),
        about: new FormControl('')
      }),
      santProfileImage: this.formBuilder.group({
        avatar: new FormControl('')
      })
    });
    this.sanghInformationForm = this.formBuilder.group({
      sanghInfoForm: this.formBuilder.group({
        sangh_type: new FormControl('', Validators.required),
        name: new FormControl('', Validators.required),
        mulnayak_bhagwan_name: new FormControl('', Validators.required),
        dharma_id: new FormControl(''),
        sampraday_id: new FormControl(''),
        reg_number: new FormControl(''),
        reg_date: new FormControl(''),
        type_of_template: new FormControl(null, Validators.required)
      }),
      sanghAddressForm: this.formBuilder.group({
        address: new FormControl('', Validators.required),
        city: new FormControl('', Validators.required),
        state: new FormControl('', Validators.required),
        email: new FormControl('', Validators.email),
        mobile_number: new FormControl('', Validators.required),
        phoneCode: new FormControl(null),
        sangh_website: new FormControl(''),
        apartment_name: new FormControl(''),
        number_of_members: new FormControl(null, Validators.required),
        longitude: new FormControl(''),
        latitude: new FormControl(''),
        about: new FormControl(''),
      }),
      sanghFacilitiesForm: this.formBuilder.group({
        aaradhana_bhavan_status: new FormControl(1, Validators.required),
        pathshala_status: new FormControl(1, Validators.required),
        no_of_students: new FormControl('', Validators.required),
        library_status: new FormControl(1, Validators.required),
        library_name: new FormControl('', Validators.required),
        librarian_name: new FormControl('', Validators.required),
        librarian_mobile_number: new FormControl('', Validators.required),
        phoneCode: new FormControl(null, Validators.required),
        number_of_books: new FormControl('', Validators.required),
        tifin_service_status: new FormControl(1, Validators.required),
        bhojanshala_status: new FormControl(1, Validators.required),
        aayambilshala_status: new FormControl(1, Validators.required),
        mahila_mandal_status: new FormControl(1, Validators.required),
        yuva_mandal_status: new FormControl(1, Validators.required),
        swapna_chadawa_organised_status: new FormControl(1, Validators.required),
      }),
      sanghDonationForm: this.formBuilder.group({
        bank_account_number: new FormControl(null, accountNumberValidator),
        bank_ifsc: new FormControl(''),
        bank_branch: new FormControl(''),
        bank_upi_id: new FormControl(''),
        bank_qr_code: new FormControl(null),
        bank_qr_code_url: new FormControl(null),
      }),
      sanghProfileImage: this.formBuilder.group({
        avatar: new FormControl('', Validators.required),
      })
    });
    this.certificateInformationForm = this.formBuilder.group({
      certificateInfoForm: this.formBuilder.group({
        hospital: new FormControl('', [Validators.required]),
        fullName: new FormControl('', [Validators.required]),
        email: new FormControl('', [Validators.required, Validators.email]),
        address: new FormControl('', [Validators.required]),
        aadharNumber: new FormControl('', [Validators.required, aadharValidator]),
        medicalDescription: new FormControl(null, [Validators.required]),
      }),

      certificateTermConditionInfoForm: this.formBuilder.group({
        termCondition: new FormControl(false)
      })
    });


    this.sanghMemberForm = this.formBuilder.group({
      memberId: new FormControl('123', [Validators.required]),
      fullName: new FormControl('', [Validators.required]),
      address: new FormControl('', [Validators.required]),
      profile_url: new FormControl(''),
      profile: new FormControl(null)
    });

  }

  get santInfoForm() {
    return this.santInformationForm.get('santInfoForm') as FormGroup<SantInformationForm>;
  }

  get dikshaInfoForm() {
    return this.santInformationForm.get('dikshaInfoForm') as FormGroup<DikshaInformationForm>;
  }

  get sansarikJeevanInfoForm() {
    return this.santInformationForm.get('sansarikJeevanInfoForm') as FormGroup<SansarikJeevanInformationForm>;
  }

  get santProfileImage() {
    return this.santInformationForm.get('santProfileImage') as FormGroup<SantProfileImageForm>;
  }

  get sanghInfoForm() {
    return this.sanghInformationForm.get('sanghInfoForm') as FormGroup<SanghInformationForm>;
  }

  get sanghAddressForm() {
    return this.sanghInformationForm.get('sanghAddressForm') as FormGroup<SanghAddressForm>;
  }

  get sanghProfileImage() {
    return this.sanghInformationForm.get('sanghProfileImage') as FormGroup<SanghProfileImageForm>;
  }

  get certificateForm() {
    return this.certificateInformationForm.get('certificateInfoForm') as FormGroup<CertificateForm>;
  }

  get certificateTermConditionForm() {
    return this.certificateInformationForm.get('certificateTermConditionInfoForm') as FormGroup<CertificateTermConditionForm>;
  }

  get sanghFacilitiesForm() {
    return this.sanghInformationForm.get('sanghFacilitiesForm') as FormGroup<SanghFacilitiesForm>;
  }

  get sanghDonationForm() {
    return this.sanghInformationForm.get('sanghDonationForm') as FormGroup<SanghDonationForm>;
  }

  get memberCardForm() {
    return this.sanghMemberForm as FormGroup<SanghMemberForm>;
  }

}
