import focusTrap from './Modal.FocusTrap';

let listOfWrapperElementsToFloatingUI = [];
let pageElementReference;
let isKeyDownEventListenerConfigured = false;

let modalImpl = {

    init(modalObj){
      
      
        this.modalroot = modalObj.modalrootNode;
        this.floatinguicontainerelement = modalObj.wrapperObjForFloatingUI;
        this.UID = modalObj.UID;
        this.functionReferenceToFocus = modalObj.focusDialogReference ? modalObj.focusDialogReference :false;
        this.functionReferenceToClose = modalObj.closeDialogReference;
        this.isCloseOnEscapeNeeded = modalObj.isCloseOnEscapeNeeded;
        this.isTrapFocusNeeded = modalObj.isTrapFocusNeeded;
        this.pageElementNode = modalObj.pageElementNode;
        this.name=modalObj.name;
      
    },
    
    persistFloatingUIInfoInList() {
    listOfWrapperElementsToFloatingUI.push({
      [this.UID]: {
        modalroot: this.modalroot,
        floatinguicontainerelement: this.floatinguicontainerelement,
        functionReferenceToFocus:this.functionReferenceToFocus,
        functionReferenceToClose: this.functionReferenceToClose,
        isOverlayNeeded: !!this.freezeLayer
      }
    });
    console.log(listOfWrapperElementsToFloatingUI);
  },
  bindKeyDownEventToDocument() {
    let modalImplInstance = this;
    if (!this.checkStatusOfKeyDownEventBoundToDocument()) {
      document.addEventListener('keydown', (event)=> {
        this.handleKeyPressEvent(event,modalImplInstance);
      }, true);
      this.setKeyDownEventBoundToDocumentStatusToTrue();
    
    }
  },
 handleKeyPressEvent(event,modalImplInstance) {
    if (event.eventPhase === 3) {
      return false;
    }
    console.log('event.key in modal behaviour' , event.key,modalImplInstance);
    if (event.key === 'Escape' && modalImplInstance.isCloseOnEscapeNeeded) {

      
      let floatingDialogContinerClassName =
      modalImplInstance.getClassNameOfClosestContainerElement(event.target);
      let indexOfContainerElement = modalImplInstance.getIndexOfContainerElement(
        floatingDialogContinerClassName
      );
      if (floatingDialogContinerClassName !== undefined && modalImplInstance.isCloseOnEscapeNeeded) {
        modalImplInstance.invokeCloseCallBackFunctionOfDialogElement(
          indexOfContainerElement,
          floatingDialogContinerClassName
        );
      }  
      }
     else if (event.key === 'Tab' ) {
      modalImplInstance.isTabKeyPressed = true;
      let floatingDialogContinerClassName =
      modalImplInstance.getClassNameOfClosestContainerElement(event.target);
      if (floatingDialogContinerClassName !== undefined) {
        let indexOfContainerElement = modalImplInstance.getIndexOfContainerElement(
          floatingDialogContinerClassName
        );
        if(modalImplInstance.isTrapFocusNeeded)
        {
          focusTrap(event,listOfWrapperElementsToFloatingUI[indexOfContainerElement][
            floatingDialogContinerClassName
          ].floatinguicontainerelement );     
        }
      }
    }
  },

  checkStatusOfKeyDownEventBoundToDocument() {
    return isKeyDownEventListenerConfigured;
  },
  setKeyDownEventBoundToDocumentStatusToTrue() {
    isKeyDownEventListenerConfigured = true;
  },
  
  applyFocusToFloatingDialogElement() {
    if(this.functionReferenceToFocus)
    {
    let indexOfContainerElement = this.getIndexOfContainerElement(this.UID);

    let floatingContainerObj =
      listOfWrapperElementsToFloatingUI[indexOfContainerElement][this.UID];
      console.log(typeof floatingContainerObj.functionReferenceToFocus);
      floatingContainerObj.functionReferenceToFocus();
    }
  },
  getClassNameOfClosestContainerElement(elemObj) {
    if (elemObj.closest('div[class^=floatingcontainer_]') !== null) {
      return elemObj.closest('div[class^=floatingcontainer_]').className;
    }
  },

  getClassNameOfContainerElement(index) {
    let containerObj = listOfWrapperElementsToFloatingUI[index];
    return Object.keys(containerObj)[0];
  },

  getContainerElement(index) {
    let containerObj = listOfWrapperElementsToFloatingUI[index];
    return containerObj[Object.keys(containerObj)[0]];
  },

  getIndexOfContainerElement(valueToMatch) {
    let  that = this;
    return listOfWrapperElementsToFloatingUI.findIndex(function (element) {
      return that.findIndexOfContainerElement(element, valueToMatch);
    });
  },
  findIndexOfContainerElement(element, floatingDialogContinerClassName) {
    return Object.keys(element)[0] == floatingDialogContinerClassName;
  },

  removeFloatingUIContainerElement(index, refClass) {
    if (Object.keys(listOfWrapperElementsToFloatingUI[index])[0] === refClass) {
      let floatingContainerObj =
        listOfWrapperElementsToFloatingUI[index][refClass];

      let floatingContainerElem =
        floatingContainerObj.floatinguicontainerelement;

      let modalRoot = floatingContainerObj.modalroot;
      modalRoot.removeChild(floatingContainerElem);
      listOfWrapperElementsToFloatingUI.splice(index, 1);

      if (listOfWrapperElementsToFloatingUI.length === 0) {
        this.resetModalBoxValues();
      } else {
        let topElem = this.getContainerElement(
          listOfWrapperElementsToFloatingUI.length - 1
        );
        topElem.functionReferenceToFocus && topElem.functionReferenceToFocus();
      }
    }
  },

  invokeCloseCallBackFunctionOfDialogElement(indexOfDialogElement, refClass) {
    listOfWrapperElementsToFloatingUI[indexOfDialogElement][
      refClass
    ].functionReferenceToClose();
  },

  getTopMostFloatingUIContainerElement(refClass) {
    let lengthOfUIContainerElement = listOfWrapperElementsToFloatingUI.length;

    return listOfWrapperElementsToFloatingUI[lengthOfUIContainerElement - 1][
      refClass
    ].floatinguicontainerelement;
  },
  getFloatingUIContainerElement(index, refClass) {
    return listOfWrapperElementsToFloatingUI[index][refClass]
      .floatinguicontainerelement;
  },


  persistPageElementReference() {
    if (typeof this.pageElementNode !== 'undefined') {
      pageElementReference = this.pageElementNode;
    }
  },
  applyFocusToPageElementReference() {
    pageElementReference.current.focus();
  },
  resetPageElementReference() {
    pageElementReference = null;
  },

  
  resetisEventListenerAddedProperty() {
    isKeyDownEventListenerConfigured = false;
  },
  unbindKeyDownEventListener() {
    document.removeEventListener('keydown', this.handleKeyPressEvent);
  },
  resetWrapperElementsList()
  {
    console.log(this);  
    listOfWrapperElementsToFloatingUI=[];
  },
  resetModalBoxValues() {
    this.resetWrapperElementsList();
    this.applyFocusToPageElementReference();
    this.resetPageElementReference();
    this.resetisEventListenerAddedProperty();
    this.unbindKeyDownEventListener();
  }
  };
  export default modalImpl;
