import { useContext } from 'react'
import { Flex, Button, Divider } from 'antd'
import { ElectronContext } from '../context/ElectronContext'
import XMarkIcon from '../../Icons/XMarkIcon'
import MinusIcon from '../../Icons/MinusIcon'
import ContractIcon from '../../Icons/ContractIcon'
import ExpandIcon from '../../Icons/ExpandIcon'
import MacOSTrafficLights from './MacOSTrafficLights'

const DashboardWindowButtons = () => {
  const { isMaximized, handleWindowControl, platform, isFullScreen } =
    useContext(ElectronContext)

  const closeButton = (
    <Button
      icon={<XMarkIcon />}
      type={'text'}
      onClick={() => handleWindowControl('close')}
    />
  )
  const minimizeButton = (
    <Button
      icon={<MinusIcon />}
      type={'text'}
      onClick={() => handleWindowControl('minimize')}
    />
  )
  const fullscreenButton = (
    <Button
      icon={isFullScreen ? <ContractIcon /> : <ExpandIcon />}
      type={'text'}
      onClick={() => handleWindowControl('fullscreen')}
    />
  )
  const maximizeButton = (
    <Button
      icon={isMaximized ? <ContractIcon /> : <ExpandIcon />}
      type={'text'}
      onClick={() => handleWindowControl('maximize')}
    />
  )

  const customWindowControls = (
    <div
      className='electrobun-webkit-app-region-no-drag'
      style={{ width: '95px', marginRight: '2.5px' }}
    >
      <Flex style={{ position: 'relative', zIndex: 9999 }}>
        {platform == 'darwin' ? (
          <>
            {closeButton}
            {minimizeButton}
            {fullscreenButton}
          </>
        ) : (
          <>
            {minimizeButton}
            {maximizeButton}
            {closeButton}
          </>
        )}
      </Flex>
    </div>
  )

  return (
    <Flex align='center'>
      {platform == 'darwin' ? (
        <>
          <MacOSTrafficLights />
          <Divider
            type='vertical'
            style={{ height: '14px', margin: '3px 6px 0 0' }}
          />
        </>
      ) : (
        customWindowControls
      )}
    </Flex>
  )
}

export default DashboardWindowButtons
