import {AfterViewInit, Component, Input, OnInit, ViewChild} from '@angular/core';
import {ChipRemoveEvent} from '@progress/kendo-angular-buttons';
import {RewardService} from '../service/reward.service';
import {RewardExModel} from '../models/reward-ex.model';
import {PayOutExModel} from '../models/payout.ex.model';
import {PayOutService} from '../service/payout.service';
import {PeopleSelectComponent} from '../people-select/people-select.component';
import {AppConfig} from '../app.config';
import {UserExModel} from '../models/userExModel';
import {SessionService} from '../service/session.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-pay-out-details',
  templateUrl: './pay-out-details.component.html',
  styleUrls: ['./pay-out-details.component.scss']
})
export class PayOutDetailsComponent implements OnInit , AfterViewInit{
  @Input() Id = 0;
  @Input() public model: PayOutExModel = new PayOutExModel();

  @ViewChild('selectPeople', {static: false}) sp: PeopleSelectComponent;

  subTotal = 0;
  tax = 0;
  Total = 0;

  hintSelectRewardsButton = false;
  hinted = false;

  rewards: RewardExModel[] = [];
  constructor(private rs: RewardService, private payOutService: PayOutService,
              private cfg: AppConfig,  private ss: SessionService, private router: Router) { }

  ngOnInit(): void {
    if (this.Id > 0){
      this.payOutService.getPayOutEx(this.Id).subscribe(
        resp => {
          this.model = new PayOutExModel(resp);
          this.rewards = this.model.Rewards;
          this.updateTotal(this.model.Rewards);
      });
    }
  }

  ngAfterViewInit(): void {
    if ( this.Id === 0 ) {
      this.sp.showDropDown();
    }
  }

  public photoBackground(id: string): string {
    return 'url(' + this.cfg.getUrl('avatar/' + id) + ')';
  }

  public onChangeRewardList( list: RewardExModel[]): void {
    this.rewards = list;
    this.model.Rewards = this.rewards;
    this.payOutService.savePayOut(this.model.toPayOutModel()).subscribe( resp => {
      this.model = new PayOutExModel(resp);
    });
    this.updateTotal(list);
  }

  private updateTotal(list: RewardExModel[]): void {
    this.Total = 0;
    list.forEach( v => {
      this.Total += v.Amount;
    });
    this.subTotal = this.Total / 1.1;
    this.tax = this.Total - this.subTotal;
  }

  public onEditBeneficiary(): void{
    this.sp.showDropDown();
  }
  public onToChange(e: any): void {
    if ( this.model.Rewards.length === 0 && this.hinted === false ){
      this.hintSelectRewardsButton = true;
      this.hinted = true;
      setTimeout( () => {
        this.hintSelectRewardsButton = false;
      }, 2000);
    }
  }

  public onPrepared(): void {
    this.model.Prepared.Id = this.ss.CurrentUser.Id;
    this.payOutService.setPayOutPrepared(this.model.toPayOutModel()).subscribe(
      resp => {
        this.model.Prepared = new UserExModel(resp.Prepared);
        this.model.Status = resp.Status;
      }
    );
  }

  public onCancelPrepared(): void {
    this.model.Prepared.Id = '';
    this.payOutService.setPayOutUnPrepared(this.model.toPayOutModel()).subscribe(
      resp => {
        this.model.Prepared = new UserExModel(resp.Prepared);
        this.model.Status = resp.Status;
      }
    );
  }

  public onApprove(): void{
    this.model.Approved.Id = this.ss.CurrentUser.Id;
    this.payOutService.setPayOutApproved(this.model.toPayOutModel()).subscribe(
      resp => {
        this.model.Approved = new UserExModel(resp.Approved);
        this.model.Status = resp.Status;

        // prepare payment
        if ( this.model.PayDate.getFullYear() <= 1900 ){
          this.model.PayDate = new Date();
        }
        if (this.model.PayAmount === 0 && this.Total !== 0){
          this.model.PayAmount = this.subTotal;
        }

      }
    );
  }

  public onRevoke(): void {
    this.payOutService.setPayOutUnapproved(this.model.toPayOutModel()).subscribe(
      resp => {
        this.model.Approved = new UserExModel(resp.Approved);
        this.model.Status = resp.Status;

        this.model.Paid.Id = '';
        this.model.PayDate = new Date('1900-01-01');
        this.model.PayAmount = 0;
      }
    );
  }

  public onPaid(): void {
    this.model.Paid.Id = this.ss.CurrentUser.Id;
    this.payOutService.setPayOutPaid(this.model.toPayOutModel()).subscribe(
      resp => {
        this.model.Paid = new UserExModel(resp.Paid);
        this.model.Status = resp.Status;
        this.model.PayDate = new Date(resp.PayDate);
        this.model.PayAmount = resp.PayAmount;
      }
    );
  }

  public onUnPaid(): void{
    this.payOutService.setPayOutUnpaid(this.model.toPayOutModel()).subscribe(
      resp => {
        this.model.Paid = new UserExModel(resp.Paid);
        this.model.Status = resp.Status;
        this.model.PayDate = new Date(resp.PayDate);
        this.model.PayAmount = resp.PayAmount;
      }
    );
  }

  public onGotoRewardList(): void{
    this.router.navigate(['/list-all-rewards']).then();
  }
}

