import {
   Component,
   EventEmitter,
   Injector,
   Output,
   ViewChild
} from '@angular/core';
import {
   AppComponentBase
} from '@shared/common/app-component-base';
import {
   ReviewServiceProxy,
   EmployeeReviewServiceProxy,
   UserServiceProxy,
   CreateReviewInput,
   CreateEmployeeReviewInput,
   UpdateReviewInput,
   UpdateEmployeeReviewInput,
   UserListDto
} from '@shared/service-proxies/service-proxies';
import {
   ModalDirective
} from 'ngx-bootstrap';
import * as _ from 'lodash';
import {
   finalize
} from 'rxjs/operators';
import * as moment from 'moment';

@Component({
   selector: 'createOrEditEmployeeReviewModal',
   templateUrl: './create-or-edit-review.component.html',
   styles: [`.user-edit-dialog-profile-image {
            margin-bottom: 20px;
       }`]
})
export class CreateOrEditEmployeeReviewModalComponent extends AppComponentBase {

   @ViewChild('createOrEditModal', {static: false}) modal: ModalDirective;

   @Output() modalSave: EventEmitter < any > = new EventEmitter < any > ();

   isCreate: boolean = false;
   active = false;
   saving = false;

   employeeReview: {
      id: number,
      userId: number,
      reviewId: number,
      content: any,
      rating: any,
   } = <any>{};

   users: UserListDto[] = [];

   userInput: any;

   constructor(
      injector: Injector,
      private _reviewServiceProxy: ReviewServiceProxy,
      private _employeeReviewServiceProxy: EmployeeReviewServiceProxy,
      private _userServiceProxy: UserServiceProxy,
   ) {
      super(injector);
   }

   onShown(): void {
      $('.kt-select2').select2();
   }

   create(): void {

      this.saving = true;

      let reviewInput = new CreateReviewInput({
         content: this.employeeReview.content,
         rating: this.employeeReview.rating
      });

      this._reviewServiceProxy.createReview(reviewInput)
         .subscribe( 
            success=> {

               let reviewId = success;

               let input = new CreateEmployeeReviewInput({
                  reviewId: reviewId,
                  userId: Number((<HTMLInputElement>document.getElementById('userInput')).value)
               });
               
               this._employeeReviewServiceProxy.createEmployeeReview(input)
                  .pipe(finalize(() => {
                     this.saving = false;
                  }))
                  .subscribe(() => {
                     this.alertSaveSuccess();
                  });
            },
            error=> {
               this.saving = false;
            });
   }

   update(): void {

      this.saving = true;

      let input = new UpdateReviewInput({
         id: this.employeeReview.reviewId,
         content: this.employeeReview.content,
         rating: this.employeeReview.rating
      });

      this._reviewServiceProxy.updateReview(input)
         .pipe(finalize(()=> {
            this.saving = false;
         }))
         .subscribe(()=> {
            let input = new UpdateEmployeeReviewInput({
               id: this.employeeReview.id,
               reviewId: this.employeeReview.reviewId,
               userId: Number((<HTMLInputElement>document.getElementById('userInput')).value)
            });
            this._employeeReviewServiceProxy.updateEmployeeReview(input)
               .pipe(finalize(()=> {
                  this.saving = false;
               }))
               .subscribe(()=> {
                  this.alertSaveSuccess();
               });

            this.alertSaveSuccess();
         });
   }

   show(id?: any): void {
      let maxcount=1000;
      this.isCreate = id ? false: true;

      this.active = true;

            if (!this.isCreate) {
               this._employeeReviewServiceProxy
                  .getEmployeeReview(id, undefined,undefined, undefined, undefined, undefined, undefined)
                  .subscribe((result) => {
                     let data = result.items[0];
                     this.employeeReview.content = data.review.content;
                     this.employeeReview.rating = data.review.rating;
                     this.employeeReview.reviewId = data.reviewId;
                     this.userInput = data.userId;
                     this.employeeReview.id = data.id;
                     this.showAllUsers(maxcount);
                   
                     this.modal.show();
                  });
            }
            else {
               this.clearField();
               this.userInput = null;
               this.showAllUsers(maxcount);
              console.log(this.userInput);
               this.modal.show();
               
            }
   }
   
   showAllUsers(maxcount) : void {
      this.active = true;
      this._userServiceProxy.getUsers(undefined,undefined,undefined, undefined, undefined, maxcount, undefined).subscribe((result) => {
         this.users = result.items;
 
     });
 
   }

   alertSaveSuccess(): void {
      this.notify.info(this.l('SavedSuccessfully'));
      this.modalSave.emit(null);
      this.close();
   }

   clearField(): void {
      this.employeeReview.rating = null;
      this.employeeReview.content = null;
   }


   close(): void {
      this.active = false;
      this.modal.hide();
   }
}