/**
 * ModalPortal
 * @邠心vbe on 2022/02/28
 */
import React, { Component } from 'react';
import { StyleSheet, View } from 'react-native';
import Modal from 'react-native-modal';
import Dialog from './Dialog';

let modal
export default class ModalPortal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      showDialog: false,
      showLoading: false,
      showIOSLoading: false,
      children: <></>,
      loadChildren: <></>,
      loadMessage: "Loading...",
    };
    modal = this;
    this.isHide = true;
  }

  static show(children) {
    return modal.show(children);
  }

  static showLoading(children) {
    return modal.showLoading(children);
  }

  static dismiss() {
    modal.dismiss();
  }

  static dismissLoading() {
    modal.dismissLoading();
  }

  static dismissAll() {
    modal.dismissAll();
  }

  static isShowing() {
    return modal.isShowing();
  }

  show(children) {
    if (isIOS) {
      if (!this.isHide) {
        setTimeout(() => {
          this.show(children);
        }, 500);
        return;
      }
      this.setState({
        showDialog: true,
        children: children,
      }, () => {
        this.onModalShow();
      })
    } else {
      this.setState({
        showDialog: true,
        children: children
      })
    }
  }

  showLoading(children) {
    if (isIOS) {
      //console.log("showIOSLoading", children)
      this.setState({
        loadMessage: children,
        showIOSLoading: true
      })
    } else {
      this.setState({
        showLoading: true,
        loadChildren: children
      });
    }
  }

  dismiss() {
    this.setState({
      showDialog: false
    })
  }

  dismissLoading() {
    if (isIOS) {
      this.setState({
        showIOSLoading: false
      })
    } else {
      this.setState({
        showLoading: false
      })
    }
  }

  dismissAll() {
    this.setState({
      showDialog: false,
      showLoading: false,
      showIOSLoading: false
    })
  }

  isShowing() {
    //console.log("[ModalPortal] isShowing", this.state.showDialog);
    return this.state.showDialog;
  }

  onBackPress() {
    if (this.state.showLoading) {
      this.dismissLoading();
    } else if (this.state.showDialog) {
      this.dismiss();
    }
  }

  onModalShow() {
    console.log('onModalShow', this.isHide);
    this.isHide = false;
  }

  onModalHide() {
    console.log('onModalHide', this.isHide);
    this.isHide = true;
  }

  render() {
    return (
      <>
        <Modal
          style={{zIndex: 900}}
          isVisible={this.state.showDialog}
          avoidKeyboard={true}
          animationIn={"fadeIn"}
          animationOut={"fadeOut"}
          useNativeDriver={true}
          onBackButtonPress={() => this.onBackPress()}
          onModalHide={() => this.onModalHide()}
        >
          {this.state.children}
        </Modal>
        <Modal
          style={{zIndex: 900}}
          isVisible={this.state.showLoading}
          animationIn="fadeIn"
          animationOut="fadeOut"
          useNativeDriver={true}
          animationInTiming={80}
          animationOutTiming={100}
          onBackButtonPress={() => this.onBackPress()}
          //onModalHide={() => this.onModalHide()}
        >
          {this.state.loadChildren}
        </Modal>
        { this.state.showIOSLoading &&
          <View style={styles.iosLoadingView}>
            <Dialog.IOSProgress message={this.state.loadMessage}/>
          </View>
        }
      </>
    );
  }
}

const styles = StyleSheet.create({
  iosLoadingView: {
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    zIndex: 500,
    alignItems: 'center',
    position: 'absolute',
    justifyContent: 'center',
    backgroundColor: 'rgba(0,0,0,.7)'
  }
})