import { Context } from '@kit.AbilityKit';
import { IModule, MODULE_NAME } from '../../../IModule';
import { UIExtensionsManager } from '../../../UIExtensionsManager';
import { DocInfoViewModel } from './DocInfoView'
import { FoxitRDKNative, PDFViewCtrlModel } from 'foxit_rdk';
import { BaseItemImpl } from '../../../control/toolbar/impl/BaseItemImpl';
import { TB_Position } from '../../../control/toolbar/impl/BaseBar';
import { DeviceType, TopBarStyle } from '../../../common/Constants';
import { BOTTOM_TOOL_BAR_ITEM,
  TOP_TOOL_ATTACH_BAR_ITEM,
  TOP_TOOL_BAR_ITEM, TOP_TOOL_BAR_TAB } from  '../../../control/toolbar/ToolbarItemConfig'
import { IViewEventListener } from '../../../event/IViewEventListener';
import { MoreMenuModule } from '../../more/MoreMenuModule';
import { MoreConstants } from '../../more/MoreMenuConstants'
import { IInteractionEventListener } from '../../../event/IInteractionEventListener';
import { display } from '@kit.ArkUI';
import { FSDisplayUtil } from '../../../utils/FSDisplayUtil';
import { IMenuEventListener } from '../../../event/IMenuEventListener';
import { ArrayList } from '@kit.ArkTS';

@Observed
export class DocInfoModule implements IModule {
  private mContext: Context;
  private mUiExtManager: UIExtensionsManager;
  private mFilePath: string | null = null;
  private mDocInfoViewModel: DocInfoViewModel | null = null;
  private docViewEvents:ArrayList<IDocViewEvent> = new ArrayList();
  private left: number = 0;
  private right: number = 0;

  constructor(context: Context, uiExtensionsManager:UIExtensionsManager) {
    this.mContext = context;
    this.mUiExtManager = uiExtensionsManager;
  }

  getName(): string {
    return MODULE_NAME.DOCINFO;
  }

  loadModule(): boolean {
    this.mDocInfoViewModel = new DocInfoViewModel(this.mContext, this.mUiExtManager.getPDFViewCtrlModel());
    this.mDocInfoViewModel.init(this.mFilePath);
    // this.mUiExtManager.registerMenuEventListener(this.menuEventListener);
    this.mUiExtManager.registerInteractionListener(this.interactionListener);
    this.mUiExtManager.registerModule(this);
    return true;
  }

  getView(): DocInfoViewModel | null {
    return this.mDocInfoViewModel
  }

  unloadModule(): boolean {
    // this.mUiExtManager.unregisterMenuEventListener(this.menuEventListener);
    this.mUiExtManager.unregisterInteractionListener(this.interactionListener);
    return true;
  }

  getFilePath(): string | null {
    return this.mFilePath;
  }

  setFilePath(filePath: string): void {
    this.mFilePath = filePath;
    this.mDocInfoViewModel?.setFilePath(this.mFilePath);
  }

  unSetDocInfoStoreage(): void {
    AppStorage.delete(MoreConstants.SP_KEY_DOC_INFO_PARAM);
  }


  private interactionListener: IInteractionEventListener = {
    onBackPressed: () => {
      return false;
    },

    onConfigurationUpdate: (newConfig: Configuration) => {
    },

    onScreenRotated: (orientation: display.Orientation) => {
      this.updateSafeArea();
    }
  }

  private menuEventListener:IMenuEventListener = {
    onTriggerDismissMenu: (): void => {
      this.onTriggerDismissMenu();
    }
  }

  private updateSafeArea(): void {
    if (DeviceType.IS_PHONE) {
      FSDisplayUtil.getInstance().getCutoutHeight(this, (height: number, orientation?: display.Orientation) => {
        let left = 0;
        let right = 0;
        if (orientation == display.Orientation.LANDSCAPE) {
          right = height;
          left = 0;
        } else if (orientation == display.Orientation.LANDSCAPE_INVERTED) {
          left = height;
          right = 0;
        }

        this.left = left;
        this.right = right;
        this.onUpdateSafeArea();
      });
    }
  }


  registerDocViewEvent(docView: IDocViewEvent): void {
    if(!this.docViewEvents.has(docView)) {
      this.docViewEvents.add(docView);
    }
  }

  unregisterDocViewEvent(docView: IDocViewEvent): void {
    this.docViewEvents.remove(docView);
  }

  private onUpdateSafeArea():void{
    for (let index = 0; index < this.docViewEvents.length; index++) {
      const element = this.docViewEvents[index];
      element.updateSafeArea(this.left, this.right);
    }
  }

  private onTriggerDismissMenu():void{
    for (let index = 0; index < this.docViewEvents.length; index++) {
      const element = this.docViewEvents[index];
      element.onTriggerDismissMenu();
    }
  }
}


export interface IDocViewEvent {
  onTriggerDismissMenu:() => void;
  updateSafeArea: (leftOffset: number, rightOffset: number) => void;
}