<script>
import cBtna from "./c-btna";
import { mapGetters, mapActions } from "vuex"
import metaInfoUtils from '../classes/metaInfoUtils'

export default {
  extends: cBtna,
  data: function() {
    return {
      componentClass: "c-btn-glogin",
      client_id: null,
      auth2: null
    }
  },
  props: {
    dataFieldIdent4ExtSignInInfo: {
      type: String,
      default: "googleSignInInfo"
    },
    dataFieldIdent4ExtSignInCallbackInfo: {
      type: String,
      default: "extSignInCallbackInfo"
    }
  },
  computed: {
    ...mapGetters("csAppWindowsUtils", [
      "pathMetaInfo"
    ]),
    getSignInInfo: function() {
      return this.getDataFieldValue(this.dataFieldIdent4ExtSignInInfo)
    }
},
  methods: {
    ...mapActions("csAppWindowsUtils", [
      "setPathMetaInfo"
    ]),
    onClick: function() {
      if (window.gapi == null) return
      window.gapi.load('auth2', this.onGapiLoad)
    },
    onGapiLoad: function() {
      if (this.client_id == null) return
      //
      this.auth2 = window.gapi.auth2.init({
        client_id: this.client_id,
        cookiepolicy: 'single_host_origin',
        scope: 'profile email'
      })
      //
      if (this.auth2.isSignedIn.get() == false) {
        this.auth2.isSignedIn.listen(this.onSignInChanged)
        this.auth2.signIn()
      } else {
        this.onSigninCompleted()
      }
    },
    onSignInChanged: function(val) {
      if(val === true) {
        this.onSignInCompleted()
      }
    },
    onSignInCompleted: function() {
      let currentUserProfile = this.auth2.currentUser.get().getBasicProfile()
      if(currentUserProfile == null) return
      //
      let extSignInUserInfo = {
        id: currentUserProfile.getId(),
        name: currentUserProfile.getGivenName(),
        surname: currentUserProfile.getFamilyName(),
        frName2: currentUserProfile.getName(),
        email: currentUserProfile.getEmail(),
        photoUrl: currentUserProfile.getImageUrl()
      }
      this.setDataFieldValue({ dataFieldIdent: this.dataFieldIdent4ExtSignInCallbackInfo, dataFieldValue: JSON.stringify(extSignInUserInfo)})
      //
      this.$emit('beforeExec')
      if (this.$scopedSlots.confirmation && !this.confirmed) {
        this.showConfirmation = true
      } else {
        let payload = {
          appWindowIdent: this.appWindowIdent,
          dataSetIdent: this.dataSetIdent,
          actionIdent: this.actionIdent,
          row: this.contextRow,
          router: this.$router,
          window: window,
          layoutIdent: this.layoutIdent
        }
        this.showConfirmation = false
        this.confirmed = false
        this.invokeAction(payload)
      }
    },
    signInInfoAdd: function() {
      if(this.getSignInInfo == null || this.getSignInInfo == '') return
      //
      let signInInfo = JSON.parse(this.getSignInInfo)?.signInInfo
      if(signInInfo == null) return
      //
      this.client_id = signInInfo.client_id;
      this.metaInfoAdd(signInInfo.metaInfo)
    },
    metaInfoAdd: function(metaInfo) {
      if(metaInfo == null) return
      //
      let path = this.$route?.path
      if (path == null) return
      let pathMetaInfo = this.pathMetaInfo(path)

      pathMetaInfo = metaInfoUtils.metaInfoMerge(pathMetaInfo, metaInfo)

      this.setPathMetaInfo({
        path: this.$route?.path,
        metaInfo: { ...pathMetaInfo }
      })
    },




    /*
    metaInfoMerge: function(metaInfo, metaInfoToAdd) {
      if(metaInfoToAdd == null) return
      //
      if (metaInfo == null) {
        metaInfo = metaInfoToAdd
      } else {
        if (metaInfo.meta == null) {
          metaInfo.meta = metaInfoToAdd.meta
        } else if (metaInfoToAdd.meta != null && metaInfoToAdd.meta.length > 0) {
          let metaInfoMetas = []
          metaInfo.meta.forEach(meta => {
            metaInfoMetas.push(JSON.stringify(meta))
          })
          metaInfoToAdd.meta.forEach(meta => {
            if(metaInfoMetas.indexOf(JSON.stringify(meta) == -1)) {
              metaInfo.meta.push(meta)
            }
          })
        }
        //
        if (metaInfo.script == null) {
          metaInfo.script = metaInfoToAdd.script
        } else if (metaInfoToAdd.script != null && metaInfoToAdd.script.length > 0) {
          let metaInfoScripts = []
          metaInfo.script.forEach(script => {
            metaInfoScripts.push(JSON.stringify(script))
          })
          metaInfoToAdd.script.forEach(script => {
            if (metaInfoScripts.indexOf(JSON.stringify(script)) == -1) {
              metaInfo.script.push(script)
            }
          })
        }
        //
      }
      return metaInfo
    }
    */

  },
  watch: {
    getSignInInfo: function() {
      this.signInInfoAdd()
    }
  },
  mounted: async function() {
    if (this.getAppWindow(this.appWindowIdent) == null) {
      await this.addAppWindow({
        appWindowIdent: this.appWindowIdent,
        setActive: false,
        lookupDef: null,
        addRoutingData: false,
        path: null,
        query: null
      })
    }
    this.width = this.$el.offsetHeight
    //
    this.signInInfoAdd()
  },
  updated: function() {
    this.width = this.$el.offsetHeight
  }
}
</script>