{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
 - will be removed soon
 -}
module Haskengl.Internal.Math.Classes where

import Relude

import Foreign.Storable

import qualified Haskengl.Internal.Bindings.OpenGL as GL
import           Haskengl.Internal.Types

class Vertex a where
  toPoint :: a -> Point
  fromPoint :: Point -> Maybe a
  fromPointLossy :: Point -> a

instance Vertex Point where
  toPoint point = point
  fromPoint point = Just point
  fromPointLossy point = point

instance Vertex (GL.Color4 GL.GLfloat) where
  toPoint (GL.Color4 x y z k) = Point x y z k
  fromPoint (Point x y z k) = Just $ GL.Color4 x y z k
  fromPointLossy (Point x y z k) = GL.Color4 x y z k

instance Vertex (GL.Vertex4 GL.GLfloat) where
  toPoint (GL.Vertex4 x y z k) = Point x y z k
  fromPoint (Point x y z k) = Just $ GL.Vertex4 x y z k
  fromPointLossy (Point x y z k) = GL.Vertex4 x y z k

instance Vertex (GL.Vertex3 GL.GLfloat) where
  toPoint (GL.Vertex3 x y z) = Point x y z 0
  fromPoint (Point x y z 0) = Just $ GL.Vertex3 x y z
  fromPoint (Point x y z _) = Nothing
  fromPointLossy (Point x y z _) = GL.Vertex3 x y z

instance Vertex (GL.Vertex2 GL.GLfloat) where
  toPoint (GL.Vertex2 x y) = Point x y 0 0
  fromPoint (Point x y 0 0) = Just $ GL.Vertex2 x y
  fromPoint (Point x y _ _) = Nothing
  fromPointLossy (Point x y _ _) = GL.Vertex2 x y
