import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { MatTableDataSource } from '@angular/material/table';
import { ActivatedRoute } from '@angular/router';
import { lastValueFrom } from 'rxjs';
import { ArtitleDetailsOrder, HistoryOrder } from 'src/app/interfaces/acquisition-management/order.interface';
import { GetOrderDetails, ResponseDataOrderDetails } from 'src/app/interfaces/acquisition-management/order.interface';
import { OrderService } from 'src/app/services/acquisition-management/order.service';
import { EncService } from 'src/app/services/enc/enc.service';
import { ResourcesService } from 'src/app/services/resources/resources.service';
import { InformationProviderComponent } from '../../../provider/providers/information-provider/information-provider.component';
import { ViewDetailsLineComponent } from '../../shopping/view-details-line/view-details-line.component';
import { DetailsStateOrderComponent } from '../details-state-order/details-state-order.component';

@Component({
  selector: 'app-details-order',
  templateUrl: './details-order.component.html',
  styleUrls: ['./details-order.component.css']
})
export class DetailsOrderComponent implements OnInit {

  public displayedColumnsArtitle: string[] = ['IMAGEN','ARTI_NOMB','INAR_MODE','INAR_CODI','ARSE_PRTO','ARSE_CANT','INAR_MONE','ACCIONES'];
  public displayedColumnsState: string[] = ['HIOR_ESOR','HIOR_POEN','HIOR_FERE','ACCIONES'];

  public dataSourceArtitles: MatTableDataSource<ArtitleDetailsOrder>;
  public dataSourceState: MatTableDataSource<HistoryOrder>;

  public orderDetails: GetOrderDetails;

  public numberOrder: string;
  public numberProvider: string;

  public isLoading: boolean;

  constructor(
    public resourcesService: ResourcesService,
    private _activatedRouter: ActivatedRoute,
    private _encService: EncService,
    private _orderService: OrderService,
    private _dialog: MatDialog,
  ) {
    this.numberOrder = '';
    this.numberProvider = '';

    this.dataSourceArtitles = new MatTableDataSource<ArtitleDetailsOrder>();
    this.dataSourceState = new MatTableDataSource<HistoryOrder>();

    this.isLoading = false;

    this.orderDetails = {} as GetOrderDetails;
  }

  ngOnInit(): void {
    this.getDataURL();
  }

  private async getDataURL() {
    let encOrderNumber = this._activatedRouter.snapshot.paramMap.get('numberOrder')!;
    this.numberOrder = await this._encService.decrypt(encOrderNumber)!;

    let encProviderNumber = this._activatedRouter.snapshot.paramMap.get('numberProvider')!;
    this.numberProvider = await this._encService.decrypt(encProviderNumber)!;

    this.getDetailsOrder(encOrderNumber, encProviderNumber);
  }

  private async getDetailsOrder(encOrderNumber: string, encProviderNumber: string) {
    this.isLoading = true;
    await lastValueFrom(this._orderService.getOrderByNumber(encOrderNumber, encProviderNumber)).then(
      (responseData: ResponseDataOrderDetails) => {
        if ( !responseData.error ) {
          
          this.orderDetails = responseData.response;
          this.dataSourceArtitles.data = this.orderDetails.ARTI;
          this.dataSourceState.data = this.orderDetails.HIST;
        } else {
          this.resourcesService.openSnackBar(responseData.msg);
        }
      }, (error: HttpErrorResponse) => this.resourcesService.checkErrors(error)
    );
    this.isLoading = false;
  }

  public openDialog( typeDialog: string, dataElement: any = null ) {
    if ( typeDialog === 'VIEW_PROVIDER' ) {
      this._dialog.open(InformationProviderComponent, {
        width: '1200px',
        disableClose: true,
        data: this.orderDetails.PROV_NUPR,
      });
    } else if ( typeDialog === 'VIEW_ARTITLE' ) {
      let dataArtitle: ArtitleDetailsOrder = dataElement;
      this._dialog.open(ViewDetailsLineComponent, {
        width: '1200px',
        disableClose: true,
        data: dataArtitle!.INAR_IDIN,
      });
    } else if ( typeDialog === 'VIEW_STATE' ) {
      let dataState: HistoryOrder = dataElement;
      this._dialog.open(DetailsStateOrderComponent, {
        width: dataState.HIOR_EVID === null ? '500px' : '900px',
        disableClose: true,
        data: dataState,
      });
    }
  }

  public setDirection() :string {
    let direction: string = '';

    if ( this.orderDetails !== null ) {
      direction += this.orderDetails.DESP_CALL + ', ';
      direction += this.orderDetails.DESP_NUEX;
      direction += this.orderDetails.DESP_NUIN === null || this.orderDetails.DESP_NUIN === undefined ? ', ' : ' ';
      direction += this.orderDetails.DESP_NUIN === null || this.orderDetails.DESP_NUIN === undefined ? '' : this.orderDetails.DESP_NUIN + ', ';
      direction += this.orderDetails.DESP_COPO + ', ';
      direction += this.orderDetails.DESP_COLO + '. ';
      direction += this.orderDetails.DESP_MUNI + ', ';
      direction += this.orderDetails.DESP_ENTI + ', ';
      direction += this.orderDetails.DESP_PAIS + '.';

      return direction;
    } else {
      return '';
    }
  }
}
