import { AuthServiceService } from './../../shared/auth-service.service';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { UserService } from './../../shared/user.service';
import { Component, OnInit, HostListener } from '@angular/core';
import { NgxSpinnerService } from 'ngx-spinner';


@Component({
  selector: 'app-service-item',
  templateUrl: './service-item.component.html',
  styleUrls: ['./service-item.component.css']
})

export class ServiceItemComponent implements OnInit {

  userId: number;
  servicesId: number;
  changeText: boolean;
  mouseover: boolean;
  services = [];
  pages = [];
  mobilePages = [];
  extraPages = [];
  defaultPageId: number;
  myInnerHeight = window.innerHeight;

  isHovering = false;

  mouseHovering() {
      this.isHovering = true;
      console.log(this.isHovering);
  }
  mouseLeaving() {
      this.isHovering = false;
      console.log(this.isHovering);
  }

  constructor( private authService:AuthServiceService,
    private userSer: UserService,
    private router: Router,
    private route: ActivatedRoute,
    private userservice: UserService,
    private authSer: AuthServiceService,
    private spinner: NgxSpinnerService) { }



  ngOnInit() {

    this.extraPages = [];
    //get profile data
    this.userSer.getUserDataProfile();
    this.changeText = false;
    //show spinner
    this.spinner.show();
    this.authSer.arabicTemplate = true;
    //hide search header
    this.authService.showSearchHeader = false;

    //this.userSer.getServicesData();
    this.route.params.subscribe(
      (params: Params) => {
        this.userId = params['userID'],
        this.servicesId = params['serviceID']
      }
    );

    this.userSer.getServicesData(this.userId).subscribe(
      (responce) => {
        console.log(responce);
        this.services = responce['services'];
        this.spinner.hide();
      },
      (error) => {console.log(error)}
    );

    this.userSer.getPagesPermetiotns(this.userId, this.servicesId).subscribe(
      (responce) => {
        this.mobilePages = responce['pages'];
        console.log(responce['pages'].length);
        if(responce['pages'].length > 6) {
          for(let i = 0; i < 6 ; i++) {
            this.pages[i] = responce['pages'][i];
          }
          for(let i = 6; i < responce['pages'].length; i++) {
            this.extraPages.push(responce['pages'][i]);
          }
          this.authSer.showMoreLinks = true;
        } else {
          this.authSer.showMoreLinks = false;
          this.pages = responce['pages'];
          // this.pages[0].active = 1;
        }
        this.spinner.hide();
        console.log('extraaaaa' , this.extraPages);
        console.log('paggges' , this.pages);
      },
      (error) => {console.log(error)}
    );

  }

  //click on service in side button
  onGetService(service) {
    this.mobilePages = [];
    if(service.id == 1) {
      //خدمه الصلاحيات
      this.userservice.getPagesPermetiotns(this.userId, service.id).subscribe(
        (responce) => {
          console.log(responce);
          this.mobilePages = responce['pages'];
          this.pages = responce['pages'];
          this.authSer.showMoreLinks = false;
          console.log(this.pages);
          //this.pages[0].active = 1;
          if(this.pages[0].id == 1) {
            console.log('/service/' + this.userId + '/' + service.id + '/users/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/users/' + this.pages[0].id]);
          }else if(this.pages[0].id == 2) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/roles/' + this.pages[0].id]);
          } else if(this.pages[0].id == 3) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/departments/' + this.pages[0].id]);
          } else if(this.pages[0].id == 4) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/sections/' + this.pages[0].id]);
          } else if(this.pages[0].id == 5) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/reports/' + this.pages[0].id]);
          }
          this.spinner.hide();
        },
        (error) => {console.log(error)}
      );

    } else if(service.id == 6 || service.id == 2) {
      //خدمه اداره المحتوي
      this.extraPages = [];
      this.userservice.getPagesPermetiotns(this.userId, service.id).subscribe(
        (responce) => {
          console.log(responce);
          this.mobilePages = responce['pages'];
          if(responce['pages'].length > 6) {
            for(let i = 0; i < 6 ; i++) {
              this.pages[i] = responce['pages'][i];
            }
            for(let i = 6; i < responce['pages'].length; i++) {
              this.extraPages.push(responce['pages'][i]);
            }
            this.authSer.showMoreLinks = true;
          } else {
            this.pages = responce['pages'];
            this.authSer.showMoreLinks = false;
          }
          console.log(this.pages);
          //this.pages[0].active = 1;
          if(this.pages[0].id == 6) {
            console.log('/service/' + this.userId + '/' + service.id + '/hospitalsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/hospitalsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 10) {
            console.log('/service/' + this.userId + '/' + service.id + '/mangementList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/mangementList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 8){
            console.log('/service/' + this.userId + '/' + service.id + '/eventsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/eventsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 9) {
            console.log('/service/' + this.userId + '/' + service.id + '/joinUs/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/joinUs/' + this.pages[0].id]);
          } else if(this.pages[0].id == 7 || this.pages[0].id == 19) {
            console.log('/service/' + this.userId + '/' + service.id + '/externalsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/externalsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 11) {
            console.log('/service/' + this.userId + '/' + service.id + '/achievementsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/achievementsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 12) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/tabs/' + this.pages[0].id]);
          } else if(this.pages[0].id == 13, this.pages[0].id == 20) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/newsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 16 || this.pages[0].id == 29) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/footerList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 14) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/internalServicesList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 15) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/lecturesList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 17 || this.pages[0].id == 18) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/mainMenuList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 24) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/advertisimentList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 30) {
            console.log('/service/' + this.userId + '/' + service.id + '/hospitalsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/hospitalsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 21) {
            console.log('/service/' + this.userId + '/' + service.id + '/barEventsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/barEventsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 32) {
            console.log('/service/' + this.userId + '/' + service.id + '/contactUsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/contactUsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 23) {
            console.log('/service/' + this.userId + '/' + service.id + '/visitingTimesList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/visitingTimesList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 25) {
            console.log('/service/' + this.userId + '/' + service.id + '/eventCalendarsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/eventCalendarsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 27) {
            console.log('/service/' + this.userId + '/' + service.id + '/externalAchievementsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/externalAchievementsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 26) {
            console.log('/service/' + this.userId + '/' + service.id + '/statisticsList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/statisticsList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 22) {
            console.log('/service/' + this.userId + '/' + service.id + '/advertisingList/' + this.pages[0].id);
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/advertisingList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 28) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/map/' + this.pages[0].id]);
          }
          this.spinner.hide();
        },
        (error) => {
          console.log(error)
        }
      );
    } else if(service.id == 3) {
      //خدمه التدريب
      this.userservice.getPagesPermetiotns(this.userId, service.id).subscribe(
        (responce) => {
          console.log(responce);
          if(this.pages[0].id == 33) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/supervisorList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 34) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/absorpationList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 35) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/reviewTraineeData/' + this.pages[0].id]);
          } else if(this.pages[0].id == 36) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/reviewSectionForTrainee/' + this.pages[0].id]);
          } else if(this.pages[0].id == 37) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/printCardTraineeList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 38) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/registrationTraineeList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 39){
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/finalAccerditationList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 40) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/trainnerList/' + this.pages[0].id]);
          }
        },
        (error) => {
          console.log(error);
        }
      );
    } else if(service.id == 4) {
      //خدمه الحركه
      this.userservice.getPagesPermetiotns(this.userId, service.id).subscribe(
        (responce) => {
          console.log(responce);
          if(this.pages[0].id == 43) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/vehicleList/' + this.pages[0].id]);
          } else if(this.pages[0].id == 44) {
            this.router.navigate(['/service/' + this.userId + '/' + service.id + '/vehicleModelList/' + this.pages[0].id]);
          }
        },
        (error) => {
          console.log(error);
        }
      );
    }
  }

  //go to component of page
  getListPage(dataPage){

    for(let i = 0; i< this.pages.length; i++) {
      if(this.pages[i].id == dataPage.id) {
        this.pages[i].active = 1;
      } else if(this.pages[0].id != dataPage.id) {
        this.pages[i].active = 0;
      } else {
       this.pages[i].active = 0;
      }
    }

    console.log(dataPage);
    if(dataPage.id == 1) {
      this.router.navigate(['users/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 2) {
      this.router.navigate(['roles/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 3) {
      this.router.navigate(['departments/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 4) {
      this.router.navigate(['sections/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 5) {
      this.router.navigate(['reports'], {relativeTo: this.route});
    } else if(dataPage.id == 6 || dataPage.id == 30) {
      this.router.navigate(['hospitalsList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 10 || dataPage.id == 31){
      this.router.navigate(['mangementList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 8) {
      this.router.navigate(['eventsList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 9){
      this.router.navigate(['joinUs/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 7 || dataPage.id == 19) {
      this.router.navigate(['externalsList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 11) {
      this.router.navigate(['achievementsList/' + dataPage.id], {relativeTo: this.route});
    }else if(dataPage.id == 12) {
      this.router.navigate(['tabs/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 13 || dataPage.id == 20) {
      this.router.navigate(['newsList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 16 || dataPage.id == 29) {
      this.router.navigate(['footerList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 14) {
      this.router.navigate(['internalServicesList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 15) {
      this.router.navigate(['lecturesList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 17 || dataPage.id == 18){
      this.router.navigate(['mainMenuList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 24) {
      this.router.navigate(['advertisimentList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 21) {
      this.router.navigate(['barEventsList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 32) {
      this.router.navigate(['contactUsList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 23) {
      this.router.navigate(['visitingTimesList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 25) {
      this.router.navigate(['eventCalendarsList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 27) {
      this.router.navigate(['externalAchievementsList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 26) {
      this.router.navigate(['statisticsList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 22) {
      this.router.navigate(['advertisingList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 28) {
      this.router.navigate(['map/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 33) {
      this.router.navigate(['supervisorList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 34) {
      this.router.navigate(['absorpationList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 35) {
      this.router.navigate(['reviewTrainerData/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 36){
      this.router.navigate(['reviewSectionForTrainee/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 37) {
      this.router.navigate(['printCardTraineeList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 38) {
      this.router.navigate(['registrationTraineeList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 39){
      this.router.navigate(['finalAccerditationList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 40) {
      this.router.navigate(['trainnerList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 43) {
      this.router.navigate(['vehicleList/' + dataPage.id], {relativeTo: this.route});
    }else if(dataPage.id == 44){
      this.router.navigate(['vehicleModelList/' + dataPage.id], {relativeTo: this.route});
    } else if(dataPage.id == 45) {
      this.router.navigate(['registrationVehicleList/' + dataPage.id], {relativeTo: this.route});
    }
  }

  onAddUser() {
    this.router.navigate(['addUser'],  {relativeTo: this.route});
  }

  goToHome() {
    this.router.navigate(['services/' + this.userId]);
  }

}
