/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { ChangeDetectionStrategy, ChangeDetectorRef, Component, HostListener, OnDestroy, OnInit } from '@angular/core';
import { publishedSymbol } from '@zeppelin/core';
import { isRecord } from '@zeppelin/utility';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

import { HeliumService, MessageService } from '@zeppelin/services';
import { setTheme } from '@zeppelin/visualizations';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'zeppelin-workspace',
  templateUrl: './workspace.component.html',
  styleUrls: ['./workspace.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class WorkspaceComponent implements OnInit, OnDestroy {
  private destroy$ = new Subject();
  private messageId: string | null = null;
  publishMode = false;

  constructor(
    public messageService: MessageService,
    private cdr: ChangeDetectorRef,
    private nzMessageService: NzMessageService,
    private heliumService: HeliumService
  ) {}

  onActivate(component: unknown) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    this.publishMode = !!(isRecord(component) && publishedSymbol in component && component[publishedSymbol as any]);
    this.cdr.markForCheck();
  }

  /**
   * Close the old connection manually when the network is offline
   * and connect a new, the {@link MessageService} will auto-retry
   */
  @HostListener('window:offline')
  onOffline() {
    this.messageService.close();
    this.messageService.connect();
  }

  setUpWebsocketReconnectMessage() {
    this.messageService.connectedStatus$.pipe(takeUntil(this.destroy$)).subscribe(data => {
      if (!data) {
        if (this.messageId === null) {
          this.messageId = this.nzMessageService.loading('Connecting WebSocket ...', { nzDuration: 0 }).messageId;
        }
      } else if (this.messageId !== null) {
        this.nzMessageService.remove(this.messageId);
        this.messageId = null;
      }
      this.cdr.markForCheck();
    });
  }

  ngOnInit() {
    setTheme();
    this.setUpWebsocketReconnectMessage();
    this.heliumService.initPackages();
  }

  ngOnDestroy(): void {
    this.destroy$.next();
    this.destroy$.complete();
  }
}
