const BASE_STYLES = `buttonSelector {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  font-family: Inter, Helvetica, Arial, sans-serif;
  font-size: 16px;
  padding: 12px 32px 12px 24px;
  gap: 8px;
  border-radius: 8px;
  width: fit-content;
  text-decoration: none;
  cursor: pointer;
}
buttonSelector span {
  color: inherit;
  font-family: inherit;
  font-size: inherit;
}
buttonSelector span:nth-child(1)  {
  line-height: 0;
}

buttonSelector.light {
  background: #FFFFFF;
  color: #20252C;
  border: 1px solid #E0E4EB;
  box-shadow: 0px 4px 8px rgba(0, 17, 51, 0.05);
}

buttonSelector.dark {
  background: #20252C;
  color: #FFFFFF;
  border: 1px solid #E0E4EB;
  box-shadow: 0px 4px 8px rgba(0, 17, 51, 0.05);
}
`

const TITLE = 'Login via Sumsub'

const svgLight = `
<svg xmlns="http://www.w3.org/2000/svg" width="34" height="24" viewBox="0 0 248 182" fill="none">
  <path fill-rule="evenodd" clip-rule="evenodd" d="M33.0667 113.028C33.0667 114.569 34.3161 115.819 35.8568 115.819H63.3424C64.8832 115.819 66.1326 114.569 66.1326 113.028V99.2732H79.8765C81.4173 99.2732 82.6667 98.2432 82.6667 96.7025V85.5208C82.6667 83.98 81.4173 82.7306 79.8765 82.7306H35.8568C34.3161 82.7306 33.0667 83.98 33.0667 85.5208V113.028ZM33.0659 52.43C33.0659 50.8893 31.8165 49.6399 30.2758 49.6399L2.79013 49.6399C1.24941 49.6399 0 50.8893 0 52.43V79.9377C0 81.4784 1.24941 82.7278 2.79013 82.7278L13.7257 82.7278C15.2664 82.7278 16.5333 81.4784 16.5333 79.9377V66.1825H30.2758C31.8165 66.1825 33.0659 65.1397 33.0659 63.5989V52.43ZM49.6 151.702C49.6 150.161 48.3456 148.912 46.8049 148.912L35.8456 148.912C34.3048 148.912 33.0667 150.161 33.0667 151.702V162.626C33.0667 164.167 34.3048 165.455 35.8456 165.455L46.8049 165.454C48.3456 165.454 49.6 164.167 49.6 162.626V151.702ZM245.21 49.6399L217.724 49.6399C216.184 49.6399 214.934 50.8893 214.934 52.43V63.3923C214.934 64.9331 216.184 66.1825 217.724 66.1825H231.467V79.9377C231.467 81.4784 232.717 82.7278 234.258 82.7278L245.21 82.7279C246.751 82.7279 248 81.4784 248 79.9377V52.43C248 50.8893 246.751 49.6399 245.21 49.6399ZM212.143 82.7306H168.123C166.583 82.7306 165.333 83.98 165.333 85.5207V96.483C165.333 98.0237 166.583 99.2732 168.123 99.2732H181.867V113.028C181.867 114.569 182.808 115.819 184.349 115.819H212.143C213.684 115.819 214.933 114.569 214.933 113.028V85.5207C214.933 83.98 213.684 82.7306 212.143 82.7306ZM195.609 165.455H181.867V151.796C181.867 150.256 180.309 148.909 178.769 148.909H165.333V118.611C165.333 117.071 163.805 115.821 162.264 115.821H148.8V85.7783C148.8 84.2376 147.301 82.7278 145.76 82.7278H101.827C100.287 82.7278 99.2 84.2376 99.2 85.7783V115.819H85.3231C83.7819 115.819 82.6667 117.247 82.6667 118.787V148.909H68.8188C67.2781 148.909 66.1333 150.256 66.1333 151.796V165.455H52.3145C50.7738 165.455 49.6 166.76 49.6 168.301V179.225C49.6 180.766 50.7738 182 52.3145 182H79.7429C81.2836 182 82.6667 180.766 82.6667 179.225V165.455H96.4091C97.9498 165.455 99.1992 164.261 99.1992 162.72V132.364H148.8V162.664C148.8 164.205 149.8 165.455 151.34 165.455H165.333V179.21C165.333 180.751 166.304 182 167.845 182H195.609C197.15 182 198.399 180.751 198.399 179.21V168.301C198.399 166.76 197.15 165.455 195.609 165.455ZM212.114 148.912H201.19C199.649 148.912 198.4 150.161 198.4 151.702V162.662C198.4 164.202 199.649 165.455 201.19 165.455H212.114C213.655 165.455 214.933 164.202 214.933 162.662V151.702C214.933 150.161 213.655 148.912 212.114 148.912Z" fill="#001133"/>
  <path fill-rule="evenodd" clip-rule="evenodd" d="M68.9237 49.6368H179.076C180.617 49.6368 181.866 48.6529 181.866 47.111V35.8844C181.866 34.3421 180.617 33.0922 179.076 33.0922H165.333V19.339C165.333 17.7972 164.083 16.5469 162.543 16.5469L148.8 16.5476V2.79291C148.8 1.25108 147.551 0.000759544 146.01 0.000759544L101.99 2.42752e-06C100.45 2.42752e-06 99.2002 1.25032 99.2002 2.79216V16.5469H85.457C83.9163 16.5469 82.6669 17.7972 82.6669 19.339V33.0922H68.9237C67.383 33.0922 66.1335 34.3421 66.1335 35.8844V47.111C66.1335 48.6529 67.383 49.6368 68.9237 49.6368Z" fill="#1BEDBA"/>
</svg>
`

const svgDark = `
<svg xmlns="http://www.w3.org/2000/svg" width="34" height="24" viewBox="0 0 248 182" fill="none">
  <path fill-rule="evenodd" clip-rule="evenodd" d="M33.2723 111.063C33.2723 112.636 34.5294 113.911 36.0797 113.911H61.1391C62.6894 113.911 63.9466 112.636 63.9466 111.063V97.0232H77.776C79.3263 97.0232 80.5835 95.9718 80.5835 94.3992V85.6243C80.5835 84.0517 79.3263 82.7763 77.776 82.7763H36.0797C34.5294 82.7763 33.2723 84.0517 33.2723 85.6243V111.063ZM30.6744 51.848C30.6744 50.2753 29.4172 49 27.8669 49L2.80747 49C1.25718 49 0 50.2753 0 51.848V77.9467C0 79.5193 1.25718 80.7946 2.80747 80.7946L11.2139 80.7946C12.7642 80.7946 14.039 79.5193 14.039 77.9467V63.2469H27.8669C29.4172 63.2469 30.6744 62.1825 30.6744 60.6098V51.848ZM47.9605 153.053C47.9605 151.48 46.6984 150.205 45.1481 150.205L35.4191 150.205C33.8688 150.205 32.623 151.481 32.623 153.053V162.885C32.623 164.457 33.8688 165.771 35.4191 165.771L45.1481 165.771C46.6984 165.771 47.9605 164.457 47.9605 162.884V153.053ZM245.193 49L220.133 49C218.583 49 217.326 50.2753 217.326 51.848V60.3989C217.326 61.9716 218.583 63.2469 220.133 63.2469H233.962V77.9467C233.962 79.5193 235.219 80.7947 236.769 80.7947L245.193 80.7947C246.743 80.7947 248 79.5194 248 77.9467V51.848C248 50.2753 246.743 49 245.193 49ZM211.92 82.7763H170.224C168.674 82.7763 167.416 84.0516 167.416 85.6243V94.1752C167.416 95.7479 168.674 97.0231 170.224 97.0231H184.053V111.063C184.053 112.636 185 113.911 186.55 113.911H211.92C213.471 113.911 214.728 112.636 214.728 111.063V85.6243C214.728 84.0516 213.471 82.7763 211.92 82.7763ZM195.405 167.75H180.928V153.809C180.928 152.236 179.361 150.862 177.811 150.862H164.292V119.937C164.292 118.364 162.754 117.089 161.204 117.089H147.656V86.4232C147.656 84.8506 146.147 83.3095 144.597 83.3095H102.988C101.438 83.3095 100.344 84.8506 100.344 86.4232V117.086H86.3812C84.8305 117.086 83.7082 118.543 83.7082 120.116V150.862H69.7743C68.224 150.862 67.0721 152.236 67.0721 153.809V167.75H52.5181C50.9678 167.75 49.7867 169.083 49.7867 170.655V179.167C49.7867 180.74 50.9678 182 52.5181 182H77.5199C79.0702 182 80.4618 180.74 80.4618 179.167V165.112H94.939C96.4893 165.112 97.7465 163.894 97.7465 162.321V131.336H150.253V162.264C150.253 163.837 151.259 165.112 152.809 165.112H167.538V179.152C167.538 180.725 168.515 182 170.065 182H195.405C196.955 182 198.213 180.725 198.213 179.152V170.655C198.213 169.083 196.955 167.75 195.405 167.75ZM212.54 150.205H202.847C201.297 150.205 200.039 151.481 200.039 153.053V162.921C200.039 164.493 201.297 165.771 202.847 165.771H212.54C214.091 165.771 215.377 164.493 215.377 162.921V153.053C215.377 151.481 214.091 150.205 212.54 150.205Z" fill="white"/>
  <path fill-rule="evenodd" clip-rule="evenodd" d="M68.8221 47H177.178C178.736 47 180 46.018 180 44.4792V35.8545C180 34.3152 178.736 33.0678 177.178 33.0678H163.522V19.5834C163.522 18.0446 162.258 16.7967 160.7 16.7967L146.514 16.7975V2.78742C146.514 1.24862 145.25 0.000755631 143.692 0.000755631L102.309 0C100.75 0 99.4867 1.24786 99.4867 2.78667V16.7967H85.8306C84.2722 16.7967 83.0085 18.0446 83.0085 19.5834V33.0678H68.8221C67.2637 33.0678 66 34.3152 66 35.8545V44.4792C66 46.018 67.2637 47 68.8221 47Z" fill="#1BEDBA"/>
</svg>
`
declare global {
  interface Window {
    isSumsubSignin: boolean
  }
}

export interface InitSumsubSigninOptions {
  baseUrl?: string
  selector: string
  applicantIdentifiersJWT?: string
  title?: string
  theme?: 'dark' | 'light'
}

type SumsubSigninCallback = (authCode: string) => void

function getUniLink(baseUrl: string, uniId: string, idJWT?: string) {
  if (baseUrl[baseUrl.length - 1] !== '/') {
    baseUrl = `${baseUrl}/`
  }
  const path = baseUrl.indexOf('localhost') === -1 ? `websdk/` : ''
  let url = `${baseUrl}${path}p/${uniId}`
  if (idJWT) {
    url += `?ids=${idJWT}&signin`
  } else {
    url += '?signin'
  }
  return url
}

class SumsubSignin {
  static instance: SumsubSignin

  static uniId: string
  static callback: SumsubSigninCallback
  static options: InitSumsubSigninOptions

  static unilink: string
  static stylesId: string
  static modal: Window | null

  constructor() {
    if (SumsubSignin.instance) {
      return SumsubSignin.instance
    }

    SumsubSignin.stylesId = 'signin-' + btoa(new Date().getTime().toString())
    SumsubSignin.instance = this
    Object.freeze(this)
  }

  init(uniId: string, callback: SumsubSigninCallback, options: InitSumsubSigninOptions) {
    SumsubSignin.uniId = uniId
    SumsubSignin.callback = callback
    SumsubSignin.options = options

    this.insertStyles()
    this.insertButton()
  }

  insertStyles() {
    const style = document.getElementById(SumsubSignin.stylesId) || document.createElement('style')
    style.id = SumsubSignin.stylesId
    style.innerHTML = BASE_STYLES.replace(/buttonSelector/g, SumsubSignin.options.selector)
    document.head.appendChild(style)
  }

  insertButton() {
    const el = document.querySelector(SumsubSignin.options.selector)
    if (!el) {
      console.error(`el .${SumsubSignin.options.selector} not found`)
      return
    }
    if (!SumsubSignin.callback) {
      console.error('callback not found')
      return
    }
    if (!SumsubSignin.options.baseUrl) {
      console.error('baseurl not found')
      return
    }

    SumsubSignin.unilink = getUniLink(
      SumsubSignin.options.baseUrl,
      SumsubSignin.uniId,
      SumsubSignin.options.applicantIdentifiersJWT
    )

    const theme = SumsubSignin.options.theme || 'light'
    el.classList.add(theme)
    el.setAttribute('href', SumsubSignin.unilink)
    el.setAttribute('rel', 'noopener noreferrer')

    const spanSvg = document.createElement('span')
    spanSvg.classList.add('button-logo')
    spanSvg.innerHTML = theme === 'light' ? svgLight : svgDark

    const spanText = document.createElement('span')
    spanText.innerText = SumsubSignin.options.title || TITLE

    el.appendChild(spanSvg)
    el.appendChild(spanText)
    el.addEventListener('click', this.openModal)
  }

  openModal(e: Event): void {
    e.preventDefault()

    const WIDTH = 500
    const HEIGHT = 800
    const left = Math.trunc((document.body.offsetWidth - WIDTH) / 2)
    const top = 100

    const params = `menubar=no,toolbar=no,location=no,status=no,left=${left},top=${top},width=${WIDTH},height=${HEIGHT},noopener=no`
    SumsubSignin.modal = window.open(SumsubSignin.unilink, TITLE, params)
    if (!SumsubSignin.modal) {
      window.alert('Error open')
      return
    }

    window.addEventListener('message', SumsubSignin.listener)
  }

  static listener(event: MessageEvent) {
    const url = new URL(SumsubSignin.unilink)
    if (event.origin !== url.origin) {
      return
    }
    const { data } = event
    if (data.method === 'onApplicantReviewComplete') {
      window.removeEventListener('message', SumsubSignin.listener)
      SumsubSignin.modal && SumsubSignin.modal.close()
      SumsubSignin.callback(data.authCode)
    }
  }
}

function initSumsubSignin(
  uniId: string,
  callback: SumsubSigninCallback,
  options: InitSumsubSigninOptions = {
    baseUrl: 'https://in.sumsub.com/',
    selector: 'sumsub-signin-btn',
    theme: 'light',
  }
): void {
  const instance = new SumsubSignin()
  instance.init(uniId, callback, options)
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.initSumsubSignin = initSumsubSignin

export default initSumsubSignin
