import { Component, OnInit } from '@angular/core';
import { C, COMMA, ENTER, F } from '@angular/cdk/keycodes';
import { MatChipInputEvent } from '@angular/material/chips';

import { CronOptions } from 'ngx-cron-editor';

import { BotService } from 'src/app/Services/bot.service';
import { HelperService } from 'src/app/Helpers/helper.service';
import { ProfileService } from 'src/app/Services/profile.service';
import { FormControl } from '@angular/forms';

export interface Share {
  isin: string;
}

export interface Keyword {
  name: string;
}

@Component({
  selector: 'app-bot-settings',
  templateUrl: './bot-settings.component.html',
  styleUrls: ['./bot-settings.component.scss'],
})
export class BotSettingsComponent implements OnInit {
  keywords: Keyword[] = [];
  shares: Share[] = [];

  constructor(
    private botService: BotService,
    private helper: HelperService,
    private profileService: ProfileService
  ) {}

  ngOnInit(): void {
    this.shares = this.helper.formatShareData();
    this.keywords = this.helper.formatKeywordsData();
  }

  addOnBlur = true;
  readonly separatorKeysCodes = [ENTER, COMMA] as const;

  async addKeyword(event: MatChipInputEvent): Promise<void> {
    const value = (event.value || '').trim();

    // Add keyword to database
    if (value && !this.keywords.includes({ name: value })) {
      console.log('Added: ' + value);
      this.botService.createKeyword(value.toLowerCase()).subscribe((result) => {
        console.log(result);
      });
    }

    // Clear the input value
    event.chipInput!.clear();

    if (value) {
      await this.helper.delay(1000);
      this.keywords = [];
      this.keywords = this.helper.formatKeywordsData();
    }
  }

  async removeKeyword(keyword: Keyword): Promise<void> {
    this.botService.deleteKeyword(keyword.name).subscribe((result) => {
      console.log(result);
    });

    await this.helper.delay(1000);

    this.keywords = [];
    this.keywords = this.helper.formatKeywordsData();
  }

  async addShare(event: MatChipInputEvent): Promise<void> {
    const value = (event.value || '').trim();

    // Add share to database
    if (value && !this.shares.includes({ isin: value.toLowerCase() })) {
      console.log('Added: ' + value);
      this.botService.createShare(value, 'Comment').subscribe((result) => {
        console.log(result);
      });
    }

    // Clear the input value
    event.chipInput!.clear();

    if (value) {
      await this.helper.delay(1000);

      this.shares = [];
      this.shares = this.helper.formatShareData();
    }
  }

  async removeShare(share: Share): Promise<void> {
    this.botService.deleteShare(share.isin).subscribe((result) => {
      console.log(result);
    });

    await this.helper.delay(1000);

    this.shares = [];
    this.shares = this.helper.formatShareData();
  }

  setCronString() {
    this.profileService
      .addCronString(this.cronForm.value)
      .subscribe((result) => {
        console.log(result);
      });
  }

  cronForm = new FormControl('0 0 1/1 * *');
  public cronOptions: CronOptions = {
    defaultTime: '00:00:00',

    hideMinutesTab: true,
    hideHourlyTab: true,
    hideDailyTab: false,
    hideWeeklyTab: true,
    hideMonthlyTab: true,
    hideYearlyTab: true,
    hideAdvancedTab: true,
    hideSpecificWeekDayTab: true,
    hideSpecificMonthWeekTab: true,

    use24HourTime: true,
    hideSeconds: true,

    cronFlavor: 'standard', //standard or quartz
  };
}
