import { Component, OnInit } from '@angular/core';
import { HitAWodService } from 'src/app/services/hit-a-wod.service';
import { SubscriptionService } from 'src/app/services/subscription.service';
import { TrainingWorkout, TrainingWorkoutSection } from 'src/app/models/training-workout.model';
import { UtilsService } from 'src/app/services/utils.service';

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

  workouts:TrainingWorkout[];
  loading_workouts:boolean = true;
  company_id:number;
  box_id:number;

  //paginator
  length:number = 0;
  pageSize:number = 7;
  page:number = 0;

  //secondary
  active_secondary:boolean = false;
  choosed_workout:TrainingWorkout;

  //filtering and sorting
  params = {
    sort_col: 'training_workouts.id',
    sort_dir: 'asc',
    name_filter: null,
    category_filter: null,
    box_filter: null,
  };
  name_filter:string;
  category_filter:string;
  box_filter:number = 1;
  colors:any;

  constructor(private subscriptionService: SubscriptionService,
              private api: HitAWodService,
              private utils: UtilsService) { }

  ngOnInit(): void {
    this.colors = this.utils.colors;
    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.getWorkouts();
      }
    });
  }

  getWorkouts() {
    this.loading_workouts = true;
    this.api.getWorkoutsBusinessIntelligence(this.company_id,this.box_id, this.pageSize, this.page, this.params).subscribe( w => {
      if (w) {
        this.workouts = w.data;
        this.length = (parseInt(w.page_size)<w.total) ? w.total : null;
        this.loading_workouts = false;
      }
    })
  }

  setSort(col:string) {
    this.page = 0;
    if(col === this.params.sort_col) {
      //estem ordenant per la mateixa, vol dir que vol canviar la direccio nomes
      if(this.params.sort_dir === "desc") this.params.sort_dir = "asc";
      else if(this.params.sort_dir === "asc") {
        this.params.sort_col = "id";
        this.params.sort_dir = "desc";
      }
    }
    else {
      //vol dir que estem ordenant per una col nova
      this.params.sort_dir = "desc";
      this.params.sort_col = col;
    }

    this.getWorkouts();
  }

  pageChange(num:number) {
    this.page = num;
    this.getWorkouts();
  }

  searchByCategory(val:string) {
    this.category_filter = val;
    if(val && val.length > 3) {
      this.params.category_filter = val;
      this.page = 0;
      this.getWorkouts();
    }
    else if(this.params.category_filter != null) {
      this.params.category_filter = null;
      this.page = 0;
      this.getWorkouts();
    }
  }

  searchByName(val:string) {
    this.name_filter = val;
    if(val && val.length > 3) {
      this.params.name_filter = val;
      this.page = 0;
      this.getWorkouts();
    }
    else if(this.params.name_filter != null) {
      this.params.name_filter = null;
      this.page = 0;
      this.getWorkouts();
    }
  }

  setBoxFilter(val:number) {
    val = val-1;
    this.box_filter = val;
    this.params.box_filter = val;
    this.page = 0;
    this.getWorkouts();
  }

  chooseWorkout(w:TrainingWorkout) {
    this.choosed_workout = w;
    this.active_secondary = true;
  }

  closeWorkout() {
    this.choosed_workout = null;
    this.active_secondary = false;
  }

  getColor(section:TrainingWorkoutSection) {
    return this.utils.getCategoryColor(section.type);
  }
}
