{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}

module Tweet.API.Config where
  import Web.Twitter.Conduit

  import Control.Applicative
  import Control.Lens
  import qualified Data.ByteString.Char8 as S8
  import qualified Data.CaseInsensitive as CI
  import qualified Data.Map as M
  import Network.HTTP.Conduit
  import qualified Network.URI as URI
  import System.Environment
  import Web.Authenticate.OAuth as OA

  getOAuthTokens :: IO (OAuth, Credential)
  getOAuthTokens = do
      consumerKey    <- getEnv' "TWEETBOT_TWITTER_API_KEY"
      consumerSecret <- getEnv' "TWEETBOT_TWITTER_API_SECRET"
      accessToken    <- getEnv' "TWEETBOT_TWITTER_ACCESS_TOKEN"
      accessSecret   <- getEnv' "TWEETBOT_TWITTER_ACCESS_TOKEN_SECRET"
      let oauth = twitterOAuth
              { oauthConsumerKey = consumerKey
              , oauthConsumerSecret = consumerSecret
              }
          cred = Credential
              [ ("oauth_token", accessToken)
              , ("oauth_token_secret", accessSecret)
              ]
      return (oauth, cred)
    where
      getEnv' = (S8.pack <$>) . getEnv

  getProxyEnv :: IO (Maybe Proxy)
  getProxyEnv = do
      env <- M.fromList . over (mapped . _1) CI.mk <$> getEnvironment
      let u = M.lookup "https_proxy" env <|>
              M.lookup "http_proxy" env <|>
              M.lookup "proxy" env >>= URI.parseURI >>= URI.uriAuthority
      return $ Proxy <$> (S8.pack . URI.uriRegName <$> u) <*> (parsePort . URI.uriPort <$> u)
    where
      parsePort :: String -> Int
      parsePort []       = 8080
      parsePort (':':xs) = read xs
      parsePort xs       = error $ "port number parse failed " ++ xs

  getTWInfoFromEnv :: IO TWInfo
  getTWInfoFromEnv = do
      pr <- getProxyEnv
      (oa, cred) <- getOAuthTokens
      return $ (setCredential oa cred def) { twProxy = pr }
