import React, { CSSProperties, useContext } from "react"

import { ProductContext } from "../contexts/ProductContext"

import { ProductContextProps } from "../interfaces/Product"
import styles from '../styles/styles.module.css'

export interface ButtonsProps {
  className?: string;
  style?: CSSProperties;
}

const Buttons = ({ className, style }: ButtonsProps) => {
  const { counter, isMaxCountReached, handleIncreaseBy }: ProductContextProps = useContext(ProductContext)

  return (
    <div
      className={`${styles.buttonsContainer} ${className ?? ''}`}
      style={style ?? {}}
    >
      <button className={styles.buttonMinus} onClick={() => handleIncreaseBy(0-1)}>-</button>
      <div className={styles.countLabel}>{ counter }</div>
      <button
        className={`${styles.buttonAdd} ${isMaxCountReached && styles.disabled}`}
        onClick={() => handleIncreaseBy(1)}
        disabled={isMaxCountReached}
      >
        +
      </button>
    </div>
  )
}

export default Buttons