{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Codec.QRCode.Mode.ECI
  ( eci
  ) where

import           Codec.QRCode.Base

import qualified Codec.QRCode.Data.ByteStreamBuilder  as BSB
import           Codec.QRCode.Data.QRSegment.Internal
import           Codec.QRCode.Data.Result

-- | Generate a segment representing an Extended Channel Interpretation
--   (ECI) designator with the specified assignment value.
eci :: Int -> Result QRSegment
eci :: Int -> Result QRSegment
eci = (ByteStreamBuilder -> QRSegment)
-> Result ByteStreamBuilder -> Result QRSegment
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteStreamBuilder -> QRSegment
constStream (Result ByteStreamBuilder -> Result QRSegment)
-> (Int -> Result ByteStreamBuilder) -> Int -> Result QRSegment
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Result ByteStreamBuilder
eciB

eciB :: Int -> Result BSB.ByteStreamBuilder
eciB :: Int -> Result ByteStreamBuilder
eciB Int
n
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0       = Result ByteStreamBuilder
forall (f :: * -> *) a. Alternative f => f a
empty
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0x80    = ByteStreamBuilder -> Result ByteStreamBuilder
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteStreamBuilder -> Result ByteStreamBuilder)
-> ByteStreamBuilder -> Result ByteStreamBuilder
forall a b. (a -> b) -> a -> b
$ Int -> Int -> ByteStreamBuilder
BSB.encodeBits Int
4 Int
0b0111 ByteStreamBuilder -> ByteStreamBuilder -> ByteStreamBuilder
forall a. Semigroup a => a -> a -> a
<> Int -> Int -> ByteStreamBuilder
BSB.encodeBits Int
8 Int
n
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0x4000  = ByteStreamBuilder -> Result ByteStreamBuilder
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteStreamBuilder -> Result ByteStreamBuilder)
-> ByteStreamBuilder -> Result ByteStreamBuilder
forall a b. (a -> b) -> a -> b
$ Int -> Int -> ByteStreamBuilder
BSB.encodeBits Int
4 Int
0b0111 ByteStreamBuilder -> ByteStreamBuilder -> ByteStreamBuilder
forall a. Semigroup a => a -> a -> a
<> Int -> Int -> ByteStreamBuilder
BSB.encodeBits Int
2 Int
0b10 ByteStreamBuilder -> ByteStreamBuilder -> ByteStreamBuilder
forall a. Semigroup a => a -> a -> a
<> Int -> Int -> ByteStreamBuilder
BSB.encodeBits (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
8Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
2) Int
n
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
1000000 = ByteStreamBuilder -> Result ByteStreamBuilder
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteStreamBuilder -> Result ByteStreamBuilder)
-> ByteStreamBuilder -> Result ByteStreamBuilder
forall a b. (a -> b) -> a -> b
$ Int -> Int -> ByteStreamBuilder
BSB.encodeBits Int
4 Int
0b0111 ByteStreamBuilder -> ByteStreamBuilder -> ByteStreamBuilder
forall a. Semigroup a => a -> a -> a
<> Int -> Int -> ByteStreamBuilder
BSB.encodeBits Int
3 Int
0b110 ByteStreamBuilder -> ByteStreamBuilder -> ByteStreamBuilder
forall a. Semigroup a => a -> a -> a
<> Int -> Int -> ByteStreamBuilder
BSB.encodeBits (Int
3Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
8Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
3) Int
n
  | Bool
otherwise   = Result ByteStreamBuilder
forall (f :: * -> *) a. Alternative f => f a
empty