import { Layout } from 'antd'
import classnames from 'classnames'
import Loading from 'components/Loading'
import { useWindowSize } from 'hooks'
import React, { ReactNode, Suspense, useState, MouseEvent } from 'react'
import { ContainerProvider } from 'utils/container'

import './EmptyLayoutWithSidebar.scss'
import useNavUser from 'hooks/useNavUser'
import useNavMenu from 'hooks/useNavMenu'

import { MenuItem, Navbar, ToastContextProvider } from '@komi-app/creator-ui'
import { RequestAProfileModal } from 'components/PreviewLayoutHeader/RequestAProfileModal'
import { SEGMENT_EVENTS } from '../../constants/segment'
import { getNavBarTrackingProperties } from '../PreviewLayout/PreviewLayout'
import { useAnalytics } from '../../hooks/useAnalytics'
import { FLAGS, useFeatureIsOn } from '@komi-app/flags-sdk'
import { useCreatorTracking, CreatorEvent } from '@komi-app/analytics-sdk'

interface IProps {
  children?: ReactNode
  isDarkMode?: boolean
  className?: string
}

const { Content } = Layout

const EmptyLayoutWithSidebar = ({
  children,
  className = '',
  isDarkMode
}: IProps) => {
  const { height } = useWindowSize()
  const { sendSegmentEvent } = useAnalytics()

  const layoutClass = classnames({
    'main-layout': true,
    'main-layout--dark-mode': isDarkMode,
    [className]: true
  })

  const mainLayoutContentClass = classnames(
    'main-layout__content',
    'main-layout-creator-ui'
  )

  //move to new comp with navbar?
  const [requestNewProfileModalIsOpen, setRequestNewProfileModalIsOpen] =
    useState(false)

  const navUser = useNavUser()
  const navMenu = useNavMenu()
  const useAnalyticsSDK = useFeatureIsOn(FLAGS.USE_ANALYTICS_SDK_TALENT)
  const { createTracker } = useCreatorTracking()
  const navBarClick = createTracker(CreatorEvent.NAV_BAR_CLICK)

  // Navlink Handler, that sends Segment events before redirecting to link <- also replaces OnClickAdmin Dashboard
  const onClickItem = (e: MouseEvent, item: MenuItem) => {
    const properties = getNavBarTrackingProperties(item)

    useAnalyticsSDK
      ? navBarClick({
          type: properties?.Type,
          name: properties?.Name
        })
      : sendSegmentEvent(SEGMENT_EVENTS.NAV_BAR_CLICK, properties)

    if ('key' in item) {
      const { key } = item

      switch (key) {
        case 'request-profile':
          return setRequestNewProfileModalIsOpen(true)
      }
    }
  }

  return (
    <Layout
      className={layoutClass}
      style={{
        minHeight: `${height}px`
      }}
    >
      <div className="navbar-styles">
        <Navbar onClickItem={onClickItem} User={navUser} Menu={navMenu} />
        <RequestAProfileModal
          visible={requestNewProfileModalIsOpen}
          onCancel={() => setRequestNewProfileModalIsOpen(false)}
          onOk={() => setRequestNewProfileModalIsOpen(false)}
        />
      </div>
      <Content>
        <Layout className={mainLayoutContentClass}>
          <div className="d--flex flex--1">
            <ContainerProvider>
              <Content>
                <ToastContextProvider>
                  <Suspense fallback={<Loading />}>{children}</Suspense>
                </ToastContextProvider>
              </Content>
            </ContainerProvider>
          </div>
        </Layout>
      </Content>
    </Layout>
  )
}

export default EmptyLayoutWithSidebar
