import json

from .client import insert_many, get_all

data = [(
	"CJK Unified Ideographs",
	"cjk-ideo",
	"4E00-9FFF",
	"http://www.unicode.org/charts/PDF/U4E00.pdf",
	"https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block)",
	"http://jrgraphix.net/r/Unicode/4E00-9FFF",
	"CJK Unified Ideographs (4E00–9FFF) contains 20,971 basic Chinese characters in the range U+4E00 through U+9FEA. The block not only includes characters used in the Chinese writing system but also kanji used in the Japanese writing system and hanja, whose use is diminishing in Korea. Many characters in this block are used in all three writing systems, while others are in only one or two of the three. Chinese characters were also used in Vietnam's Nôm script (now obsolete). The first 20,902 characters in the block are arranged according to the Kangxi Dictionary ordering of radicals. In this system the characters written with the fewest strokes are listed first. The remaining characters were added later, and so are not in radical order."
),(
	"CJK Unified Ideographs Extension A",
	"cjk-ideo-a",
	"3400-4DBF",
	"http://www.unicode.org/charts/PDF/U3400.pdf",
	"https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_Extension_A",
	"http://jrgraphix.net/r/Unicode/3400-4DBF",
	"CJK Unified Ideographs Extension A (3400–4DBF) contains 6,582 additional characters in the range U+3400 through U+4DB5 that were added in Unicode 3.0 (1999)."
),(
	"CJK Unified Ideographs Extension B",
	"cjk-ideo-b",
	"20000-2A6DF",
	"http://www.unicode.org/charts/PDF/U20000.pdf",
	"https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_Extension_B",
	"http://jrgraphix.net/r/Unicode/20000-2A6DF",
	"CJK Unified Ideographs Extension B (20000–2A6DF) contains 42,711 characters in the range U+20000 through U+2A6D6 that were added in Unicode 3.1 (2001). These include most of the characters used in the Kangxi Dictionary that are not in the basic CJK Unified Ideographs block, as well as many Nôm characters that were formerly used to write Vietnamese."
),(
	"CJK Unified Ideographs Extension C",
	"cjk-ideo-c",
	"2A700-2B73F",
	"http://www.unicode.org/charts/PDF/U2A700.pdf",
	"https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_Extension_C",
	"",
	"CJK Unified Ideographs Extension C (2A700–2B73F) contains 4,149 characters in the range U+2A700 through U+2B734 that were added in Unicode 5.2 (2009)."
),(
	"CJK Unified Ideographs Extension D",
	"cjk-ideo-d",
	"2B740-2B81F",
	"http://www.unicode.org/charts/PDF/U2B740.pdf",
	"https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_Extension_D",
	"",
	"CJK Unified Ideographs Extension D is a Unicode block containing rare and historic CJK ideographs for Chinese, Japanese, Korean, and Vietnamese."
),(
	"CJK Unified Ideographs Extension E",
	"cjk-ideo-e",
	"2B820-2CEAF",
	"http://www.unicode.org/charts/PDF/U2B820.pdf",
	"https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_Extension_E",
	"",
	"CJK Unified Ideographs Extension E is a Unicode block containing rare and historic CJK ideographs for Chinese, Japanese, Korean, and Vietnamese."
),(
	"CJK Unified Ideographs Extension F",
	"cjk-ideo-f",
	"2CEB0-2EBEF",
	"http://www.unicode.org/charts/PDF/U2CEB0.pdf",
	"https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_Extension_F",
	"",
	"CJK Unified Ideographs Extension F is a Unicode block containing rare and historic CJK ideographs for Chinese, Japanese, Korean, and Vietnamese, as well as more than a thousand Sawndip characters for writing the Zhuang language."
),(
	"Kangxi Radicals",
	"cjk-radical",
	"2F00-2FDF",
	"http://www.unicode.org/charts/PDF/U2F00.pdf",
	"https://en.wikipedia.org/wiki/Kangxi_radical#Unicode",
	"https://en.wikipedia.org/wiki/Kangxi_radical",
	"In Unicode version 3.0 (1999), a separate Kangxi Radicals block was introduced which encodes the 214 radicals in sequence."
),(
	"CJK Radicals Supplement",
	"cjk-radical-sup",
	"2E80-2EFF",
	"http://www.unicode.org/charts/PDF/U2E80.pdf",
	"https://en.wikipedia.org/wiki/CJK_Radicals_Supplement",
	"https://en.wikipedia.org/wiki/Kangxi_radical",
	"CJK Radicals Supplement is a Unicode block containing alternative, often positional, forms of the Kangxi radicals."
),(
	"CJK Strokes",
	"cjk-stroke",
	"31C0-31EF",
	"http://www.unicode.org/charts/PDF/U31C0.pdf",
	"https://en.wikipedia.org/wiki/CJK_Strokes_(Unicode_block)",
	"https://en.wikipedia.org/wiki/Stroke_(CJKV_character)",
	"CJK Strokes is a Unicode block containing examples of each of the standard CJK stroke types."
),(
	"CJK Symbols and Punctuation",
	"cjk-punctuation",
	"3000-303F",
	"http://www.unicode.org/charts/PDF/U3000.pdf",
	"https://en.wikipedia.org/wiki/CJK_Symbols_and_Punctuation",
	"",
	"CJK Symbols and Punctuation is a Unicode block containing symbols and punctuation in the unified Chinese, Japanese and Korean script."
),(
	"Ideographic Description Characters",
	"cjk-ido-desc",
	"2FF0-2FFF",
	"http://www.unicode.org/charts/PDF/U2FF0.pdf",
	"https://en.wikipedia.org/wiki/Ideographic_Description_Characters_(Unicode_block)",
	"https://en.wikipedia.org/wiki/Chinese_character_description_language#Ideographic_Description_Sequences",
	"Ideographic Description Characters is a Unicode block containing graphic characters used for describing CJK ideographs."
),(
	"CJK_Compatibility",
	"cjk-cmpt",
	"3300-33FF",
	"http://www.unicode.org/charts/PDF/U3300.pdf",
	"https://en.wikipedia.org/wiki/CJK_Compatibility",
	"",
	"CJK Compatibility is a Unicode block containing square symbols (both CJK and Latin alphanumeric) encoded for compatibility with east Asian character sets."
),(
	"CJK Compatibility Forms",
	"cjk-cmpt-form",
	"FE30-FE4F",
	"http://www.unicode.org/charts/PDF/UFE30.pdf",
	"https://en.wikipedia.org/wiki/CJK_Compatibility_Forms",
	"",
	"CJK Compatibility Forms is a Unicode block containing vertical glyph variants for east Asian compatibility."
),(
	"CJK Compatibility Ideographs",
	"cjk-cmpt-ideo",
	"F900-FAFF",
	"http://www.unicode.org/charts/PDF/UF900.pdf",
	"https://en.wikipedia.org/wiki/CJK_Compatibility_Ideographs",
	"",
	"CJK Compatibility Ideographs is a Unicode block containing Han Ideographs that contained duplicate characters."
),(
	"CJK Compatibility Ideographs Supplement",
	"cjk-cmpt-ideo-sup",
	"2F800-2FA1F",
	"http://www.unicode.org/charts/PDF/U2F800.pdf",
	"https://en.wikipedia.org/wiki/CJK_Compatibility_Ideographs_Supplement",
	"",
	"CJK Compatibility Ideographs Supplement is a Unicode block containing Han characters used only for roundtrip compatibility mapping with planes 3, 4, 5, 6, 7, and 15 of CNS 11643-1992."
),(
	"Enclosed CJK Letters and Months",
	"cjk-enclosed",
	"3200-32FF",
	"http://www.unicode.org/charts/PDF/U3200.pdf",
	"https://en.wikipedia.org/wiki/Enclosed_CJK_Letters_and_Months",
	"",
	"Enclosed CJK Letters and Months is a Unicode block containing circled and parenthesized Katakana, Hangul, and CJK ideographs."
),(
	"Enclosed Ideographic Supplement",
	"cjk-enclosed-sup",
	"1F200-1F2FF",
	"http://www.unicode.org/charts/PDF/U1F200.pdf",
	"https://en.wikipedia.org/wiki/Enclosed_Ideographic_Supplement",
	"",
	"Enclosed Ideographic Supplement is a Unicode block containing characters for compatibility with the Japanese ARIB STD-B24 standard. It contains a squared kana word, and many CJK ideographs enclosed with squares, brackets, or circles."
),(
	"Private Use Area",
	"private-use-area",
	"E000-F8FF",
	"http://www.unicode.org/charts/PDF/UE000.pdf",
	"https://en.wikipedia.org/wiki/Private_Use_%28Unicode%29",
	"",
	"Private Use Area (PUA) is a range of code points that, by definition, will not be assigned characters by the Unicode Consortium"
)]

def get_range():
	blocks = []
	for item in data:
		blocks.append((item[1], item[2]))
	return blocks

def get_data():
	blocks = []
	for item in data:
		blocks.append({
			'name': item[0],
			'abbr': item[1],
			'range': item[2],
			'chart_url': item[3],
			'wikipedia_url': item[4],
			'ref_url': item[5],
			'description': item[6]
		})
	return blocks

class Cjkblocks(object):

	tblname = 'cjkblocks'

	fields = {
		'name': '',
		'abbr': '',
		'range': '',
		'chart_url': '',
		'wikipedia_url': '',
		'ref_url': '',
		'description': ''
	}

	insert_sql = '''INSERT INTO {tblname}
	(name, abbr, range, chart_url, wikipedia_url, ref_url, description)
	VALUES
	(
		"{bk_name}",
		"{bk_abbr}",
		"{bk_range}",
		"{bk_chart_url}",
		"{bk_wikipedia_url}",
		"{bk_ref_url}",
		"{bk_descript}"
	);'''

	@classmethod
	def generate(cls, output='list', tblname=''):
		'''
		output = dict or json or sql
		'''
		tblname = tblname if tblname else cls.tblname
		blocks = get_data()
		if output == 'json':
			return json.dumps(blocks, ensure_ascii=False)
		elif output == 'sql':
			sqls = []
			for block in blocks:
				sql = cls.insert_sql.format(
					tblname=tblname,
					bk_name=block['name'],
					bk_abbr=block['abbr'],
					bk_range=block['range'],
					bk_chart_url=block['chart_url'],
					bk_wikipedia_url=block['wikipedia_url'],
					bk_ref_url=block['ref_url'],
					bk_descript=block['description']
				)
				sqls.append(sql)
			return '\n'.join(sqls)
		else:
			return blocks

	@classmethod
	def insert(cls, conn):
		return insert_many(conn, cls.generate(), cls.tblname)

	@classmethod
	def get_all(cls, conn, sort_by='name'):
		return get_all(conn, cls.tblname, sort_by)