{-# LANGUAGE DeriveDataTypeable     #-}
{-# LANGUAGE FlexibleContexts       #-}
{-# LANGUAGE FlexibleInstances      #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses  #-}
{-# LANGUAGE TemplateHaskell        #-}
{-# LANGUAGE TypeFamilies           #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Plots.Axis.Line
-- Copyright   :  (C) 2015 Christopher Chalmers
-- License     :  BSD-style (see the file LICENSE)
-- Maintainer  :  Christopher Chalmers
-- Stability   :  experimental
-- Portability :  non-portable
--
-- The lines that make up an axis.
--
----------------------------------------------------------------------------
module Plots.Axis.Line
  ( -- * Grid lines
    AxisLine
  , HasAxisLine (..)

    -- * Axis line types
  , AxisLineType (..)

  ) where

import           Control.Lens     hiding (( # ))
import           Data.Data
import           Data.Default

import           Diagrams.Prelude
import Plots.Types

-- | Where axis line for coordinate should be drawn. The 'Default' is
--   'BoxAxisLine'.
data AxisLineType
  = BoxAxisLine
  | LeftAxisLine
  | MiddleAxisLine
  | RightAxisLine
  | NoAxisLine
  deriving (Show, Eq, Typeable)

instance Default AxisLineType where
  def = BoxAxisLine

-- | Information about position and style of axis lines.
data AxisLine v = AxisLine
  { alType      :: AxisLineType
  -- , alArrowOpts :: Maybe ArrowOpts
  , alVisible   :: Bool
  , alStyle     :: Style v Double
  } deriving Typeable

type instance V (AxisLine v) = v
type instance N (AxisLine v) = Double

-- | Class of object that have an 'AxisLine'.
class HasAxisLine f a where
  -- | Lens onto the 'AxisLine'.
  axisLine :: LensLike' f a (AxisLine (V a))

  -- | The position of the axis line around the axis.
  --
  --   'Default' is 'BoxAxisLine'.
  axisLineType :: Functor f => LensLike' f a AxisLineType
  axisLineType = axisLine . lens alType (\al sty -> al {alType = sty})

  -- | The options for if you want the axis line to have arrows at the
  --   end.
  --
  --   'Default' is 'Nothing'.
  --
  --   XXX (feature not currently implimented)
  -- axisLineArrowOpts :: Functor f => LensLike' f a (Maybe (ArrowOpts (N a)))
  -- axisLineArrowOpts = axisLine . lens alArrowOpts (\al sty -> al {alArrowOpts = sty})

  -- | The 'Style' applied to the axis line
  axisLineStyle :: Functor f => LensLike' f a (Style (V a) Double)
  axisLineStyle = axisLine . lens alStyle (\al sty -> al {alStyle = sty})

instance HasAxisLine f (AxisLine v) where
  axisLine = id

instance ApplyStyle (AxisLine v)
instance HasStyle (AxisLine v) where
  style = axisLineStyle

--   Note this is different from 'NoAxisLine'. Other parts that are
--   tied to the axis line will still be present when
--   'axisLineVisible' is 'False'. But if 'NoAxisLine' is set, there
--   never any line for those things to attach to, so they don't
--   exist.
instance HasVisibility (AxisLine v) where
  visible = lens alVisible (\al b -> al {alVisible = b})

instance Default (AxisLine v) where
  def = AxisLine
    { alType  = def
    -- , alArrowOpts = def
    , alVisible = True
    , alStyle = mempty
    }