import { isPlatformBrowser } from '@angular/common';
import { Component, Inject, OnInit, PLATFORM_ID } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Meta, Title } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';
import { OwlOptions } from 'ngx-owl-carousel-o';
import { Services } from '../../entities/services';
import { CommonService } from '../../shared/services/common/common.service';
import { GlobalService } from '../../shared/services/common/global.service';
import { VisitorsService } from '../../shared/services/common/visitors.service';
import { LinkService } from '../../shared/services/link.service';

@Component({
  selector: 'app-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss']
})
export class IndexComponent implements OnInit {
  public partnerOptions: OwlOptions = {
    loop: true,
    autoplay: true,
    mouseDrag: true,
    touchDrag: true,
    pullDrag: true,
    dots: false,
    navSpeed: 500,
    rewind: true,
    margin: 10,
    // navText: ['&#8249', '&#8250;'],
    responsive: {
      0: {
        items: 2
      },
      400: {
        items: 4
      },
      767: {
        items: 5
      },
      1200: {
        items: 6
      }
    },
    // nav: true
  }

  public testimonialOptions: OwlOptions = {
    loop: false,
    autoplay: false,
    mouseDrag: true,
    touchDrag: true,
    pullDrag: true,
    dots: true,
    navSpeed: 500,
    rewind: true,
    margin: 10,
    // navText: ['&#8249', '&#8250;'],
    responsive: {
      0: {
        items: 1
      },
      400: {
        items: 1
      },
      767: {
        items: 1
      },
      1200: {
        items: 1
      }
    },
    // nav: true
  }

  public HomeBannerOptions: OwlOptions = {
    loop: true,
    autoplay: true,
    mouseDrag: true,
    touchDrag: true,
    pullDrag: true,
    dots: true,
    navSpeed: 500,
    rewind: true,
    margin: 10,
    // navText: ['&#8249', '&#8250;'],
    responsive: {
      0: {
        items: 1
      },
      400: {
        items: 1
      },
      767: {
        items: 1
      },
      1200: {
        items: 1
      }
    },
    // nav: true
  }
  ipaddress: string = '';
  latitude: string = '';
  longitude: string = '';
  currency: string = '';
  currencysymbol: string = '';
  isp: string = '';
  city: string = '';
  country: string = '';
  province: string = '';
  public payment: string = "0.00";
  public interest: string = "0.00";
  public amountPayable: string = "0.00";
  public displayAmount: string = "0.00";
  public test: any
  public emiCalculatorForm: FormGroup;
  // public title:string;

  constructor(
    private formBuilder: FormBuilder,
    private commonService: CommonService,
    private meta: Meta,
    private title: Title,
    private route: ActivatedRoute,
    @Inject(PLATFORM_ID) public platformId: object,
    private linkService: LinkService
  ) {
    let city;
    if (isPlatformBrowser(this.platformId))
      city = sessionStorage.getItem("selectedCity");
    if (city)
      this.city = city;
    this.emiCalculatorForm = formBuilder.group({
      'amount': [null, [Validators.required]],
      'interest_rate': [null, [Validators.required]],
      'months': [null, [Validators.required]],
    })
    // else
    //   this.city = "surat";
  }

  async ngOnInit() {
    this.title.setTitle(this.route.snapshot.data['title']);
    this.linkService.createLinkForCanonicalURL(window.location.href);
    // this.title= `Instant Personal Loan in India - Apply Loan Online | Credit`
    this.meta.addTags([
      // {
      //   name: 'title', content: `Instant Personal Loan in India - Apply Loan Online | Credit`
      // },
      {
        name: 'description', content: `Apply for an instant personal loan online with Credit and get approved at the lowest interest rate. We are the solution to all your 'money needs!`
      },
      {
        name: 'author', content: `Credit`
      },
      {
        name: 'og:locale', property: "og:locale", content: `en_US`
      },
      {
        name: 'og:title', property: "og:title", content: `Instant Personal Loan in India - Apply Loan Online | Credit`
      },
      {
        name: 'og:description', property: "og:description", content: `Apply for an instant personal loan online with Credit and get approved at the lowest interest rate. We are the solution to all your 'money needs!`
      },
      {
        name: 'og:url', property: "og:url", content: 'https://credit.nsserver.in'
      },
      {
        name: 'og:site_name', property: "og:site_name", content: `Credit`
      },
      {
        name: 'og:type', property: "og:type", content: `Website`
      },
      {
        name: 'og:image', property: "og:image", content: `https://credit.nsserver.in/assets/img/logo/logo.webp`
      },
      {
        'http-equiv': "X-UA-Compatible", content: "IE=edge"
      },
      {
        name: 'copyright', content: `Copyright 2022, Credit`
      },
      {
        name: 'robots', content: `index, follow`
      },
      {
        name: 'Googlebot', content: `Index, Follow`
      },
      {
        name: 'rating', content: `Safe For Kids`
      },
      {
        name: 'tweeter:card', content: `Compare and apply loan`
      },
      {
        name: 'tweeter:title', content: `Compare and apply loan`
      },
      {
        name: 'tweeter:description', content: `Apply for an instant personal loan online with Credit and get approved at the lowest interest rate. We are the solution to all your 'money needs!`
      },
      {
        name: 'tweeter:site', content: `Credit`
      },
      {
        name: 'tweeter:image', content: `https://credit.nsserver.in/assets/img/logo/logo.webp`
      }
    ]);
    this.commonService.getServices();

    // await this.getService();
  }
  public openForm() {
    document.getElementById("myForm")!.style.display = "block";
  }
  closeForm() {
    document.getElementById("myForm")!.style.display = "none";
  }

  public calculateLoan() {

    if (this.emiCalculatorForm.value.amount && this.emiCalculatorForm.value.interest_rate && this.emiCalculatorForm.value.months) {
      let rate = this.emiCalculatorForm.value.interest_rate / (12 * 100);
      let rate1 = (Math.pow(1 + rate, this.emiCalculatorForm.value.months))
      let payment = this.emiCalculatorForm.value.amount * rate * (rate1 / (rate1 - 1));
      this.payment = payment.toFixed(2);
      this.amountPayable = (parseFloat(this.payment) * this.emiCalculatorForm.value.months).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
      this.interest = (parseFloat(this.payment) * this.emiCalculatorForm.value.months - this.emiCalculatorForm.value.amount).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
      this.displayAmount = this.emiCalculatorForm.value.amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

    }
  }
  // public async getService: Promise<Services[]>() {
  //   try {
  //     let res = await this.cityService.getCities();
  //     if (res && res.status == 200) {
  //       this.cities = res.recordList;
  //       if (this.cities && this.cities.length > 0)
  //         return this.cities;
  //       else
  //         return [];
  //     }
  //     else {
  //       return [];
  //     }
  //   } catch (error: any) {
  //     return error;
  //   }
  // }
}
