import PropTypes from 'prop-types'
import Tabs from '@mui/material/Tabs'
import Tab from '@mui/material/Tab'
import Box from '@mui/material/Box'
import Button from '@mui/material/Button'

import {Paypal, Liberapay} from '@icons-pack/react-simple-icons'
import {Elements} from '@stripe/react-stripe-js'
import {loadStripe} from '@stripe/stripe-js'

import {Grid} from '@mui/material'
import {appearance} from '../../lib/utils/stripe-style'

import CheckoutForm from './checkout-form'
import StripePayment from './stripe-payment'

const stripePromise = loadStripe(
  process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY
)

const PAYPAL_ID = process.env.NEXT_PUBLIC_PAYPAL_DONATE_ID
const LIBERAPAY_DONATE = process.env.NEXT_PUBLIC_LIBERAPAY_DONATE

function TabPanel(props) {
  const {children, value, index, ...other} = props

  return (
    <div
      role='tabpanel'
      hidden={value !== index}
      id={`simple-tabpanel-${index}`}
      aria-labelledby={`simple-tab-${index}`}
      {...other}
    >
      {value === index && (
        <Box sx={{p: 3}}>
          {children}
        </Box>
      )}
    </div>
  )
}

TabPanel.propTypes = {
  children: PropTypes.node,
  index: PropTypes.number.isRequired,
  value: PropTypes.number.isRequired,
}

function a11yProps(index) {
  return {
    id: `simple-tab-${index}`,
    'aria-controls': `simple-tabpanel-${index}`,
  }
}

export default function PaymentMethod({isLoading, paymentMethod, setPaymentMethod, selectedMontant, setSelectedMontant, validMontant, setValidMontant, prices, paymentIntent, setClientSecret, setPaymentIntent, setIsLoading, setPaymentIsReady, setClientEmail, clientEmail, error, setError, clientSecret}) {
  const options = {
    clientSecret,
    appearance,
  }

  const handleChange = (event, newValue) => {
    setPaymentMethod(newValue)
  }

  return (
    <Box sx={{width: '100%'}}>
      <Box sx={{borderBottom: 1, borderColor: 'divider'}}>
        <Tabs scrollButtons allowScrollButtonsMobile value={paymentMethod} aria-label='basic tabs example' variant='fullWidth' onChange={handleChange}>
          <Tab wrapped label='Carte bancaire' {...a11yProps(0)} />
          <Tab wrapped label='PayPal / Liberapay' {...a11yProps(1)} />
          <Tab wrapped label='Adhésion' {...a11yProps(2)} />
        </Tabs>
      </Box>
      <TabPanel value={paymentMethod} index={0}>
        <StripePayment
          selectedMontant={selectedMontant}
          setSelectedMontant={setSelectedMontant}
          validMontant={validMontant}
          setValidMontant={setValidMontant}
          prices={prices}
          paymentIntent={paymentIntent}
          setClientSecret={setClientSecret}
          setPaymentIntent={setPaymentIntent}
          setIsLoading={setIsLoading}
          setPaymentIsReady={setPaymentIsReady}
          setClientEmail={setClientEmail}
          clientEmail={clientEmail}
          error={error}
          setError={setError}
          isLoading={isLoading}
        />

        {clientSecret && validMontant && paymentMethod === 0 && (
          <Elements options={options} stripe={stripePromise}>
            <CheckoutForm
              prices={prices}
              validMontant={validMontant}
              setPaymentIsReady={setPaymentIsReady}
              setError={setError}
              isLoading={isLoading}
              setIsLoading={setIsLoading}
            />
          </Elements>
        )}
      </TabPanel>
      <TabPanel value={paymentMethod} index={1}>
        <Grid container rowSpacing={1}>
          <Grid item md={6} xs={12}>
            <Button variant='outlined' size='large' endIcon={<Paypal />} onClick={() => window.open(`https://www.paypal.com/donate/?hosted_button_id=${PAYPAL_ID}`, '_blank')}>
              Faire un don via PayPal
            </Button>
          </Grid>
          <Grid item md={6} xs={12}>
            <Button variant='outlined' size='large' endIcon={<Liberapay />} onClick={() => window.open(`https://liberapay.com/${LIBERAPAY_DONATE}/donate`, '_blank')}>
              Faire un don via Liberapay
            </Button>
          </Grid>
        </Grid>
      </TabPanel>
      <TabPanel value={paymentMethod} index={2}>
        <Button size='large' variant='outlined' onClick={() => window.open('https://www.helloasso.com/associations/organisation-ka-internationale/adhesions/adhesion-oki', '_blank')}>
          Cliquez ici pour adhérer à #OKi via HelloAsso
        </Button>
      </TabPanel>
    </Box>
  )
}

PaymentMethod.defaultProps = {
  selectedMontant: null,
  validMontant: null,
  paymentIntent: null,
  clientEmail: null,
  error: null,
  clientSecret: null
}

PaymentMethod.propTypes = {
  isLoading: PropTypes.bool.isRequired,
  paymentMethod: PropTypes.number.isRequired,
  setPaymentMethod: PropTypes.func.isRequired,
  selectedMontant: PropTypes.string,
  setSelectedMontant: PropTypes.func.isRequired,
  validMontant: PropTypes.string,
  setValidMontant: PropTypes.func.isRequired,
  prices: PropTypes.array.isRequired,
  paymentIntent: PropTypes.string,
  setClientSecret: PropTypes.func.isRequired,
  setPaymentIntent: PropTypes.func.isRequired,
  setIsLoading: PropTypes.func.isRequired,
  setPaymentIsReady: PropTypes.func.isRequired,
  setClientEmail: PropTypes.func.isRequired,
  clientEmail: PropTypes.string,
  error: PropTypes.string,
  setError: PropTypes.func.isRequired,
  clientSecret: PropTypes.string
}
