import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { Router, ActivatedRoute } from '@angular/router';

import {ChangeImageDialogComponent} from './change-image-dialog/change-image-dialog.component';
import * as moment from 'moment-timezone';

//Services
import { NotificationService } from 'src/app/core/services/notification.service';
import { apiLotService } from 'src/app/core/services/api/api.lot.service';
import { apiFavoriteService } from 'src/app/core/services/api/api.favorite.service';
import { apiSaleService } from 'src/app/core/services/api/api.sale.service';

// Models
import { LotInfo } from 'src/app/core/services/model/lotInfo.interface';
import { SaleInfo } from 'src/app/core/services/model/saleInfo.interface';

@Component({
  selector: 'app-new-favorites-page',
  templateUrl: './new-favorite-page.component.html',
  styleUrls: ['./new-favorite-page.component.css']
})
export class NewFavoritesPageComponent implements OnInit  {
  
  url: string = '';
  lotInfo: LotInfo;
  SaleInfo: SaleInfo;
  images: any[] = [];
  picture: String = "";
  date: Date;
  hour: string = "";
  buyProject: boolean = false;
  maxPrice: number = 0;
  Note: string = "";

  constructor(
    public dialog: MatDialog,
    private ActivatedRoute: ActivatedRoute,
    private router: Router,
    private notificationService: NotificationService,
    private apiLotService: apiLotService,
    private apiSaleService: apiSaleService,
    private apiFavoriteService: apiFavoriteService,) {

      this.ActivatedRoute.params.subscribe(params => {
        this.url = params['url'];
      });

      
      this.lotInfo = {
        idLotInterencheres: '',
        url: '',
        title:'',
        lotNumber: '',
        EstimateLow: 0,
        EstimateHigh: 0,
        Description: "",
        feesText: "",
        fees: "",
        saleInfo: {
          idSaleInterencheres: "",
          url: ""
        }
      };

      this.SaleInfo = {
        _id: "",
        idPlatform: "",
        platform: "",
        url: "",
        title: "",
        date: "",
        location: "",
        saleHouseName: "",
        status: ""
      };

      this.date = new Date();
    }

    ngOnInit(): void {
      console.log("url: "+this.url);

      // this.date = moment(this.SaleInfo.date).tz('Europe/Paris').toDate();
      // this.hour = moment(this.SaleInfo.date).tz('Europe/Paris').format('HH:mm');

      this.apiLotService.getLotInfo(this.url).subscribe( lotInfo => {
        console.log(lotInfo);
        this.lotInfo = lotInfo;        

        this.apiSaleService.getSaleInfos(this.lotInfo.saleInfo.url).subscribe( SaleInfo => {
          console.log(SaleInfo);
          this.SaleInfo = SaleInfo;       

          //hour    
          // Europe/Paris is the timezone of the user      
          this.date = moment(this.SaleInfo.date).tz('Europe/Paris').toDate();
          this.hour = moment(this.SaleInfo.date).tz('Europe/Paris').format('HH:mm'); 
          this.notificationService.openSnackBar("Loaded: "+lotInfo.title);
          }
        );

        }      
      );
      
      this.apiLotService.getPictures(this.url).subscribe( pictures => {
        this.images = pictures;
        this.picture = pictures[0];      
      });
      
    }

    openChangeImage(): void {
      const dialogRef = this.dialog.open(ChangeImageDialogComponent, {
        width: '300px',
        data: {images: this.images}
      });
    
      dialogRef.afterClosed().subscribe(result => {
        if (result) {
          this.picture = result;
        }
      });
    }

    cancel(): void {
      this.router.navigate(['favorites']);
    }
    
    save(): void {
      // Europe/Paris is the timezone of the user    
      let dateTime = moment.tz(`${this.date.toISOString().split('T')[0]}T${this.hour}`, 'Europe/Paris').format();
      
      this.apiFavoriteService.saveFavorite(this.lotInfo, this.SaleInfo, this.picture, dateTime, this.buyProject, this.maxPrice, this.Note).subscribe( res => {
        this.notificationService.openSnackBar("Favorite saved");
        this.router.navigate(['favorites']);
      });
    }
    
  
}

