"""
China(mainland)-specific Form helpers
"""

from __future__ import absolute_import, unicode_literals

import re

from django.forms import ValidationError
from django.forms.fields import CharField, RegexField, Select
from django.utils.translation import ugettext_lazy as _

from .cn_provinces import CN_PROVINCE_CHOICES


__all__ = (
    'CNProvinceSelect',
    'CNPostCodeField',
    'CNIDCardField',
    'CNPhoneNumberField',
    'CNCellNumberField',
)


ID_CARD_RE = r'^\d{15}(\d{2}[0-9xX])?$'
POST_CODE_RE = r'^\d{6}$'
PHONE_RE = r'^\d{3,4}-\d{7,8}(-\d+)?$'
CELL_RE = r'^1[34578]\d{9}$'

# Valid location code used in id card checking algorithm
CN_LOCATION_CODES = (
    11,  # Beijing
    12,  # Tianjin
    13,  # Hebei
    14,  # Shanxi
    15,  # Nei Mongol
    21,  # Liaoning
    22,  # Jilin
    23,  # Heilongjiang
    31,  # Shanghai
    32,  # Jiangsu
    33,  # Zhejiang
    34,  # Anhui
    35,  # Fujian
    36,  # Jiangxi
    37,  # Shandong
    41,  # Henan
    42,  # Hubei
    43,  # Hunan
    44,  # Guangdong
    45,  # Guangxi
    46,  # Hainan
    50,  # Chongqing
    51,  # Sichuan
    52,  # Guizhou
    53,  # Yunnan
    54,  # Xizang
    61,  # Shaanxi
    62,  # Gansu
    63,  # Qinghai
    64,  # Ningxia
    65,  # Xinjiang
    71,  # Taiwan
    81,  # Hong Kong
    91,  # Macao
)


class CNProvinceSelect(Select):
    """
    A select widget providing the list of provinces and districts
    in People's Republic of China as choices.
    """
    def __init__(self, attrs=None):
        super(CNProvinceSelect, self).__init__(attrs, choices=CN_PROVINCE_CHOICES)


class CNPostCodeField(RegexField):