# -*- coding: utf-8 -*-

import zlib

from constants import *
from urlhelper import UrlHelper

class UrlBuilder(object):

    def __init__(self, domains, use_https=False, sign_key=None,
                 sign_mode=SIGNATURE_MODE_QUERY,
                 shard_strategy=SHARD_STRATEGY_CRC):
        if not isinstance(domains, list):
            domains = [domains]
        self._domains = domains
        self._sign_key = sign_key
        self._sign_mode = sign_mode
        self._use_https = use_https
        self._shard_strategy = shard_strategy
        self._shard_next_index = 0

    def create_url(self, path, **parameters):
        if self._shard_strategy == SHARD_STRATEGY_CRC:
            crc = zlib.crc32(path) & 0xffffffff
            index = crc % len(self._domains)  # Deterministically choose domain
            domain = self._domains[index]

        elif self._shard_strategy == SHARD_STRATEGY_CYCLE:
            domain = self._domains[self._shard_next_index]
            self._shard_next_index = (
                self._shard_next_index + 1) % len(self._domains)

        else:
            domain = self._domains[0]

        scheme = "https" if self._use_https else "http"
        url_obj = UrlHelper(domain, path, scheme,
                            sign_key=self._sign_key, sign_mode=self._sign_mode,
                            **parameters)
        return str(url_obj)