import { Component, OnInit } from '@angular/core';
import { DataService } from 'src/app/Services/data.service';
import { MatDialog } from '@angular/material/dialog';
import { UserDialogComponent } from './user-dialog/user-dialog.component';
import { HelperService } from 'src/app/Helpers/helper.service';

export interface Stock {
  count: number;
  comment: string;
  isin: string;
  last_transaction: string;
  current_price: number;
}

var TRANSACTION_DATA: TransactionData[] = [];
var STOCK_DATA: Stock[] = [];

export interface TransactionData {
  comment: string;
  isin: string;
  time: string;
  count: number;
  price: number;
}

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss'],
})
export class DashboardComponent implements OnInit {
  constructor(
    private dataService: DataService,
    private helper: HelperService,
    public dialog: MatDialog
  ) {}

  dataSourceTransactions: TransactionData[] = [];
  dataSourceStocks: Stock[] = [];
  depotCurrentValue: number = 0;
  depotCost: number = 0;
  profit: number = 0;

  comment: string = '';
  isin: string = '';
  time: Date = new Date();
  count: number = 0.0;
  price: number = 0.0;

  getTransactions() {
    var TRANSACTION_DATA: TransactionData[] = [];
    this.dataService.getTransactionData().subscribe((response: any) => {
      var data = JSON.parse(response);
      this.depotCost = 0;
      for (let i = 0; i < data.data.length; i++) {
        this.depotCost += data.data[i].price * data.data[i].count;
        TRANSACTION_DATA.push({
          comment: data.data[i].comment,
          isin: data.data[i].isin,
          time: data.data[i].time,
          count: data.data[i].count,
          price: data.data[i].price,
        });
      }
      this.dataSourceTransactions = TRANSACTION_DATA;
      this.profit = this.depotCurrentValue + this.depotCost;
    });
  }

  ngOnInit() {
    /**
     * Function gets the stock data and pushes it in the temporary object array
     */
    this.dataService.getStockData().subscribe((response: any) => {
      var data = JSON.parse(response);
      console.log(data);
      this.depotCurrentValue = 0;
      for (let i = 0; i < data.data.length; i++) {
        this.depotCurrentValue +=
          data.data[i].current_price * data.data[i].count;
        STOCK_DATA.push({
          count: data.data[i].count,
          comment: data.data[i].comment,
          isin: data.data[i].isin,
          last_transaction: data.data[i].last_transaction,
          current_price: data.data[i].current_price,
        });
      }
      // assign data source
      this.dataSourceStocks = STOCK_DATA;
      this.profit += this.depotCurrentValue;
    });

    /**
     * Function gets the transaction data and pushes it in the temporary object array
     */
    this.dataService.getTransactionData().subscribe((response: any) => {
      var data = JSON.parse(response);
      this.depotCost = 0;
      for (let i = 0; i < data.data.length; i++) {
        this.depotCost += data.data[i].price * data.data[i].count;
        TRANSACTION_DATA.push({
          comment: data.data[i].comment,
          isin: data.data[i].isin,
          time: data.data[i].time,
          count: data.data[i].count,
          price: data.data[i].price,
        });
      }
      // assign data source
      this.dataSourceTransactions = TRANSACTION_DATA;
      this.profit += this.depotCost;
    });
  }

  // function to open the user dialog to create a new transaction
  openDialog(): void {
    const dialogRef = this.dialog.open(UserDialogComponent, {
      width: '50vw',
      data: {
        comment: this.comment,
        isin: this.isin,
        time: this.time,
        count: this.count,
        price: this.price,
      },
    });

    dialogRef.afterClosed().subscribe((result) => {
      this.helper.delay(1000);
      this.getTransactions();
    });
  }

  // assign columns for transactions to display in html, you can change order and add/remove columns
  displayedColumns: string[] = [
    'comment',
    'weight',
    'position',
    'name',
    'symbol',
  ];

  // assign columns to display in html, you can change order and add/remove columns
  displayedColumnsStocks: string[] = [
    'count',
    'comment',
    'isin',
    'current_price',
    'last_transaction',
  ];
}
