import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { TrainingWorkout, TrainingWorkoutSection, TrainingWorkoutLine } from 'src/app/models/training-workout.model';
import { HitAWodService } from 'src/app/services/hit-a-wod.service';
import { SubscriptionService } from 'src/app/services/subscription.service';
import { LinealChart } from 'src/app/models/Charts/lineal-chart.model';
import { Exercise } from 'src/app/models/exercise.model';
import { UtilsService } from 'src/app/services/utils.service';
import { UserBenchmark } from 'src/app/models/user-benchmark.model';

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

  company_id:number;
  box_id:number;
  workout:TrainingWorkout;
  benchmarks:UserBenchmark[];
  workout_chart:LinealChart;
  types:string[];
  exercises:Exercise[];

  nuevo:boolean;
  editable:boolean;

  showHelp:boolean = false;

  constructor(private subscriptionService: SubscriptionService,
              private api: HitAWodService,
              private activatedRoute: ActivatedRoute,
              private route: Router,
              private utilsService: UtilsService) { }

  ngOnInit(): void {
    const id = this.activatedRoute.snapshot.queryParamMap.get('id');
    this.subscriptionService.currentSubscription.subscribe( s => {
      if (s) {
        if (s.box) {
          this.company_id = s.box.company_id;
          this.box_id = s.box.id;
        } else {
          this.company_id = s.company.id;
          this.box_id = null;
        }
        this.getWorkout(id);
      }
    });
    this.getExercises();
  }
  
  private getWorkout(id:string){
    if(id!=null) { // Editar
      this.editable = true;
      this.nuevo = false;
      this.api.getWorkoutBusinessIntelligence(this.company_id,id,this.box_id).subscribe( w => {
        this.workout = w.workout;
        this.benchmarks = (w.benchmarks && w.benchmarks.length>0) ? w.benchmarks : null;
        this.types = w.types;
        if(w.chart) {
          this.workout_chart = new LinealChart();
          this.workout_chart.showLegend = false;
          this.workout_chart.showXAxisLabel = false;
          this.workout_chart.showYAxisLabel = false;
          this.workout_chart.colorScheme.domain = this.utilsService.getColorsArray();
          this.workout_chart.results = w.chart;
        } else this.workout_chart = null;
        this.editable = (this.box_id && this.workout.box_id && !this.workout_chart && !this.benchmarks && this.box_id === this.workout.box_id) ? true : false ;
      })
    } else { // Crear
      this.editable = true;
      this.nuevo = true;
      this.api.getWorkoutBusinessIntelligence(this.company_id,id,this.box_id).subscribe( w => {
        this.types = w.types;
        this.benchmarks = null;
        this.workout_chart = null;
      })
      this.workout = new TrainingWorkout();
      this.workout.box_id = 1;//this.box_id;
      this.workout.training_workout_sections = [new TrainingWorkoutSection()];
      this.workout.training_workout_sections[0].lines = [new TrainingWorkoutLine()];
      this.workout.training_workout_sections[0].lines[0].exercise_id = 1;
    }
  }

  addSection() {
    let s = new TrainingWorkoutSection();
    s.type = "For Time"
    let l = new TrainingWorkoutLine();
    l.exercise = new Exercise();
    l.exercise_scaled = new Exercise();
    s.lines = [l];
    this.workout.training_workout_sections.push(s);
  }

  deleteSection(i:number) {
    this.workout.training_workout_sections.splice(i,1);
  }

  addExercise(i:number) {
    let l = new TrainingWorkoutLine();
    l.exercise = new Exercise();
    l.exercise_scaled = new Exercise();
    this.workout.training_workout_sections[i].lines.push(l);
  }
  
  deleteExercise(i:number,j:number) {
    this.workout.training_workout_sections[i].lines.splice(j,1);
  }

  getExercises(name?:string){
    this.api.getAllExercises(name).subscribe( e => {
      if(e && e.data) this.exercises = e.data;
    })
  }

  filterExercise(event) {
    this.getExercises(event);
  }

  sameSC(i:number,j:number) {
    this.workout.training_workout_sections[i].lines[j].maleVal_scaled = this.workout.training_workout_sections[i].lines[j].maleVal
    this.workout.training_workout_sections[i].lines[j].femaleVal_scaled = this.workout.training_workout_sections[i].lines[j].femaleVal
    this.workout.training_workout_sections[i].lines[j].reps_scaled = this.workout.training_workout_sections[i].lines[j].reps
    this.workout.training_workout_sections[i].lines[j].seconds_scaled = this.workout.training_workout_sections[i].lines[j].seconds
    this.workout.training_workout_sections[i].lines[j].units_scaled = this.workout.training_workout_sections[i].lines[j].units
    this.workout.training_workout_sections[i].lines[j].exercise_scaled = this.workout.training_workout_sections[i].lines[j].exercise
    this.workout.training_workout_sections[i].lines[j].exercise_scaled_id = this.workout.training_workout_sections[i].lines[j].exercise_id
  }

  updateWorkout() {
    this.api.updateWorkout(this.workout).subscribe( w => {
      this.workout = w;
    });
  }

  createWorkout() {
    this.api.createWorkout(this.workout).subscribe( w => {
      this.workout = w;
      this.nuevo = false;
    });
  }

  deleteWorkout() {
    this.api.deleteWorkout(this.workout.id.toString()).subscribe( w => {
      this.workout = new TrainingWorkout();
      this.nuevo = true;
      this.route.navigateByUrl('/admin/workouts');
    });
  }

  secToMin(n:number) {
    return '00:' + Math.trunc(n/60) + ':' + ((n%60)>0 ? (n%60) : '00');
  }

  isEmom(type:string) {
    return type.includes('mom') || type.includes('MOM');
  }
}
