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 { LinkService } from '../../shared/services/link.service';

@Component({
  selector: 'app-emi-calculator',
  templateUrl: './emi-calculator.component.html',
  styleUrls: ['./emi-calculator.component.scss']
})
export class EmiCalculatorComponent implements OnInit {
  // public amount: number = 0;
  // public interest_rate: number
  // public months: number = 0;
  public payment: string = "0.00";
  //public isShow: boolean = false;
  public interest: string = "0.00";
  public amountPayable: string = "0.00";
  public displayAmount: string = "0.00";
  public test: any
  public _title: string = '';
  public city: string = '';
  public emiCalculatorForm: FormGroup;

  constructor(
    private formBuilder: FormBuilder,
    private meta: Meta,
    private title: Title,
    private route: ActivatedRoute,
    @Inject(PLATFORM_ID) public platformId: object,
    private linkService: LinkService
  ) {
    this.linkService.createLinkForCanonicalURL();
    this._title = `Try EMI Calculator for personal loan and review your income & commitments before arriving at loan amount.`
    this.route.snapshot.data['title'] = this._title;
    this.title.setTitle(this.route.snapshot.data['title']);
    // this.meta.updateTag({
    //   name: 'title', content: `Try EMI Calculator for personal loan and review your income & commitments before arriving at loan amount.`
    // });
    this.meta.updateTag({
      name: 'description', content: `Always use our car loan emi calculator or emi calculator for home loan. It makes your planning for loan lot easier as entire calculation is based upon interest rate along tenure and loan amount.`
    });
    this.meta.updateTag({
      name: 'author', content: `Credit`
    });
    this.meta.updateTag({
      name: 'og:locale', property: "og:locale", content: `en_US`
    });
    this.meta.updateTag({
      name: 'og:title', property: "og:title", content: `Try EMI Calculator for personal loan and review your income & commitments before arriving at loan amount.`
    });
    this.meta.updateTag({
      name: 'og:description', property: "og:description", content: `Always use our car loan emi calculator or emi calculator for home loan. It makes your planning for loan lot easier as entire calculation is based upon interest rate along tenure and loan amount.`
    });
    this.meta.updateTag({
      name: 'og:url', property: "og:url", content: `https://credit.nsserver.in`
    });
    this.meta.updateTag({
      name: 'og:site_name', property: "og:site_name", content: `Credit`
    });
    this.meta.updateTag({
      name: 'og:type', property: "og:type", content: `Website`
    });
    this.meta.updateTag({
      name: 'og:image', property: "og:image", content: `https://credit.nsserver.in/assets/img/logo/logo.webp`
    });
    this.meta.updateTag({
      'http-equiv': "X-UA-Compatible", content: "IE=edge"
    });
    this.meta.updateTag({
      name: 'copyright', content: `Copyright 2022, Credit`
    });
    this.meta.updateTag({
      name: 'robots', content: `index, follow`
    });
    this.meta.updateTag({
      name: 'Googlebot', content: `Index, Follow`
    });
    this.meta.updateTag({
      name: 'rating', content: `Safe For Kids`
    });
    this.meta.updateTag({
      name: 'tweeter:card', content: `compare & apply for business loan`
    });
    this.meta.updateTag({
      name: 'tweeter:title', content: `compare & apply for business loan`
    });
    this.meta.updateTag({
      name: 'tweeter:description', content: `Always use our car loan emi calculator or emi calculator for home loan. It makes your planning for loan lot easier as entire calculation is based upon interest rate along tenure and loan amount.`
    });
    this.meta.updateTag({
      name: 'tweeter:site', content: `Credit`
    });
    this.meta.updateTag({
      name: 'tweeter:image', content: `https://credit.nsserver.in/assets/img/logo/logo.webp`
    });
    // this.meta.addTags([
    //   {
    //     name: 'description', content: `Easy to compare and apply for any type of loan at Credit - compare with different banks's interest rate and select the plan with the lowest emi cost , Apply for credit card by just 1 single click, Credit is the right place to sign up as DSA that best fits your needs and helps you make the most of your purchases.`
    //   },
    //   {
    //     name: 'author', content: `Credit`
    //   },
    //   {
    //     name: 'keywords', content: `Personal Loan,Business Loan,Compare Loan,Gold Loan,Mortagage Loan,Loan agains property,Home Loan,Car Loan,Professional Loan,Education Loan,Instant Loan,MSME Loan,StartUp Loan,Credit Card,DSA Signup,Connectors,Employee,Sub DSA,Become a partner,EMI Calculator`
    //   },
    //   {
    //     name: 'og:title', property: "og:title", content: `Calculate EMI`
    //   },
    //   {
    //     name: 'og:description', property: "og:description", content: `Calculate EMI By just adding your loanAMount,Interest Rate,Loan Duration - 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: `../../../assets/img/logo/logo.webp`
    //   },
    //   {
    //     name: 'tweeter:card', content: `Calculate EMI`
    //   },
    //   {
    //     name: 'tweeter:title', content: `Calculate EMI`
    //   },
    //   {
    //     name: 'tweeter:description', content: `Calculate EMI By just adding your loanAMount,Interest Rate,Loan Duration - credit.nsserver.in`
    //   },
    //   {
    //     name: 'tweeter:site', content: `Credit`
    //   },
    //   {
    //     name: 'tweeter:image', content: `../../../assets/img/logo/logo.webp`
    //   }
    // ]);

    this.emiCalculatorForm = formBuilder.group({
      'amount': [null, [Validators.required]],
      'interest_rate': [null, [Validators.required]],
      'months': [null, [Validators.required]],
    })

  }

  ngOnInit(): void {
    // setTimeout(() => {
    //   this.isShow = false;
    // }, 1000);
    let city;
    if (isPlatformBrowser(this.platformId))
      city = sessionStorage.getItem("selectedCity");
    if (city)
      this.city = city;
  }
  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, ",");
      // this.amount = parseFloat(this.amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
      // var interest = (this.amount * (this.interest_rate * (1 + this.interest_rate) * this.months)) / (1 + this.interest_rate) * this.months - 1;
      // this.interest = interest;
      // //toFixed(2) --> demical num with 2 digits
      // var payment = ((this.amount / this.months) + interest).toFixed(2);
      // this.amountPayable = this.payment + this.interest;
      // this.amountPayable = this.amountPayable.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
      // //add a , every three digits
      // this.payment = payment.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
      // // this.amount = parseInt(this.amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
      // //render to the html
    }
  }
}
