{-# 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 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteStreamBuilder -> QRSegment
constStream 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 forall a. Ord a => a -> a -> Bool
< Int
0       = forall (f :: * -> *) a. Alternative f => f a
empty
  | Int
n forall a. Ord a => a -> a -> Bool
< Int
0x80    = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Int -> Int -> ByteStreamBuilder
BSB.encodeBits Int
4 Int
0b0111 forall a. Semigroup a => a -> a -> a
<> Int -> Int -> ByteStreamBuilder
BSB.encodeBits Int
8 Int
n
  | Int
n forall a. Ord a => a -> a -> Bool
< Int
0x4000  = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Int -> Int -> ByteStreamBuilder
BSB.encodeBits Int
4 Int
0b0111 forall a. Semigroup a => a -> a -> a
<> Int -> Int -> ByteStreamBuilder
BSB.encodeBits Int
2 Int
0b10 forall a. Semigroup a => a -> a -> a
<> Int -> Int -> ByteStreamBuilder
BSB.encodeBits (Int
2forall a. Num a => a -> a -> a
*Int
8forall a. Num a => a -> a -> a
-Int
2) Int
n
  | Int
n forall a. Ord a => a -> a -> Bool
< Int
1000000 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Int -> Int -> ByteStreamBuilder
BSB.encodeBits Int
4 Int
0b0111 forall a. Semigroup a => a -> a -> a
<> Int -> Int -> ByteStreamBuilder
BSB.encodeBits Int
3 Int
0b110 forall a. Semigroup a => a -> a -> a
<> Int -> Int -> ByteStreamBuilder
BSB.encodeBits (Int
3forall a. Num a => a -> a -> a
*Int
8forall a. Num a => a -> a -> a
-Int
3) Int
n
  | Bool
otherwise   = forall (f :: * -> *) a. Alternative f => f a
empty