import { Component, OnInit } from '@angular/core';
import { ToastrService } from 'ngx-toastr';
import { DashboardService } from 'src/app/services/builder/dashboard.service';
import { Dashboard3Service } from 'src/app/services/builder/dashboard3.service';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {

  data: any;
  rows: any[];
  error;
  chartData: any;

  constructor(private dashboard3Service: Dashboard3Service,
    private dashboardService: DashboardService,) { }

  ngOnInit(): void {
    this.getAllDash();
    this.getdashboard();
  }
  dashboardModel;
  dashboard_name;
  getAllDash(){
    this.dashboard3Service.getDynamicDashDetails().subscribe(data => {
      console.log(data);
      const dash = data[0].model;
      this.dashboardModel= JSON.parse(dash) ;
      this.dashboard_name = data.dashboard_name;

      for (let i = 0; i < this.dashboardModel.length; i++) {
        if(this.dashboardModel[i]?.charturl){
        this.dashboard3Service.getChartData(this.dashboardModel[i].charturl,this.dashboardModel[i].name).subscribe(tData => {
          console.log(tData);
          this.dashboardModel[i].featchData = tData;
        });
      }
      }
      console.log(this.dashboardModel);
    });
  }

  lineChartData: any[] = [];
  radarChartData: any[] = [];
  bubbleChartData: any[] = [];
  dashboardChartData;

  getdashboard() {
    this.chartData = [];
    this.radarChartData = [];
    this.bubbleChartData = [];
    this.lineChartData = [];
    this.dashboardService.getAllDash().subscribe(
      (data) => {
        this.data = data;
        const filteredData = this.data.filter(item => item.add_to_home);
  
        this.rows = filteredData;
  
        if (filteredData.length === 0) {
          this.error = "No data available with add_to_home set to true.";
        } else {
          this.error = "";
        }
  
        this.rows.forEach(item => {
          if (item['dashbord1_Line'] && Array.isArray(item['dashbord1_Line']) && item['dashbord1_Line'].length > 0) {
            item['dashbord1_Line'].forEach(dashboardLineItem => {
              if (dashboardLineItem.model) {
                const parsedModel = JSON.parse(dashboardLineItem.model);
                this.chartData.push(parsedModel);
  
                 this.dashboardChartData = this.chartData[0]?.dashboard; // Add a check here
  
                // if (this.dashboardChartData) {
                  for (let i = 0; i < this.dashboardChartData.length; i++) {
                    console.log(this.dashboardChartData[i]?.charturl);
                    if (this.dashboardChartData[i]?.charturl) {
                      this.dashboard3Service.getUrlChartData(this.dashboardChartData[i].table, this.dashboardChartData[i].name, this.dashboardChartData[i].xAxis,this.dashboardChartData[i].yAxis, this.dashboardChartData[i].datastore, this.dashboardChartData[i].charturl
                        ).subscribe(tData => {
                        console.log(tData);
                        this.dashboardChartData[i].featchData = tData;
                      });
                    }
                  // }
  
                  console.log(this.dashboardChartData);

                  // this.lineChartData.push({ data: parsedModel.data, label: dashboardLineItem.name });
                  console.log(dashboardLineItem.model);
                }
              }
            });
          }
        });
      },
      (error) => {
        console.error('Error fetching dashboard data:', error);
      });
  
    console.log("Chart Data:", this.chartData);
  }
}
