# coding: utf-8
"幹線と地方交通線をまたがって利用する場合の計算"

import main_line
import local_line
import utils

class MainAndLocalLine(object):
	"""幹線と地方交通線をまたがる場合 """

	main_line_class = main_line.MainLine
	local_line_class = local_line.LocalLine;

	@classmethod
	def fare(cls, main_d=0, local_d=0):
		if main_d == local_d == 0:
			return 0
		elif local_d == 0:
			return cls.main_line_fare(main_d)
		elif main_d == 0:
			return cls.local_line_fare(local_d)
		else:
			return cls.both_line_fare(main_d, local_d)

	@classmethod
	def main_line_fare(cls, main_d):
		return cls.main_line_class.fare(main_d)

	@classmethod
	def local_line_fare(cls, local_d):
		return cls.local_line_class.fare(local_d)

	@classmethod
	def both_line_fare(cls, main_d, local_d):
		if main_d + local_d <= 100:
			calc_d = main_d + local_d
			return cls.local_line_fare(calc_d)
		else:
			calc_d = main_d + utils.gisei(local_d)
			return cls.main_line_fare(calc_d)

class JrhMainAndLocalLine(MainAndLocalLine):
	"""JR北海道の幹線と地方交通線をまたがる場合"""
	
	main_line_class = main_line.JrhMainLine
	local_line_class = local_line.JrhLocalLine;

class JrsJrkMainAndLocalLine(MainAndLocalLine):
	"""JR四国、JR九州の幹線と地方交通線をまたがる場合の基底クラス"""

	s_fares = []

	@classmethod
	def local_line_fare(cls, local_d):
		return cls.both_line_fare(0, local_d)

	@classmethod
	def both_line_fare(cls, main_d, local_d):
		calc_d = main_d + utils.gisei(local_d)
		if main_d + local_d <= 100 or main_line==0:
			calc_d = main_d + utils.gisei(local_d)
			print(main_d,local_d,calc_d)
			s_fare = cls.special_fare(main_d+local_d, calc_d)
			if s_fare is not None:
				return s_fare
			
		return cls.main_line_fare(calc_d)
	
	@classmethod
	def special_fare(cls, d, calc_d):
		d = utils.ceil(d, 10) // 10
		calc_d = utils.ceil(calc_d, 10) // 10
		for s_fare in cls.s_fares:
			if len(s_fare) == 3:
				if s_fare[0] == calc_d and s_fare[1] == d:
					return s_fare[2]
			else:
				if s_fare[0] == calc_d:
					return s_fare[1]

		return None
	
class JrsMainAndLocalLine(JrsJrkMainAndLocalLine):
	"""JR四国の幹線と地方交通線をまたがる場合"""

	main_line_class = main_line.JrsMainLine

	s_fares = [
		# 旅客営業規則 第84条の3 第2号
		[4, 3, 160],
		[11, 10, 230],
		# 旅客営業規則 第77条の7 第2項
		[11, 230],
		[16, 290],
		[17, 15, 290],
		[21, 390],
		[22, 390],
		[26, 23, 480],
		[31, 28, 550],
		[36, 32, 660],
		[41, 37, 760],
		[46, 41, 850],
		[51, 46, 950],
		[61, 55, 1090],
		[71, 64, 1280],
		[81, 73, 1460]
	]

class JrkMainAndLocalLine(JrsJrkMainAndLocalLine):
	"""JR九州の幹線と地方交通線をまたがる場合"""

	main_line_class = main_line.JrkMainLine

	s_fares = [
		# 旅客営業規則 第84条の4 第2号
		[4, 3, 180],
		[11, 10, 250],
		# 旅客営業規則 第77条の8 第2項
		[11, 250],
		[16, 290],
		[17, 15, 290],
		[21, 400],
		[22, 400],
		[26, 23, 490],
		[31, 28, 600],
		[36, 32, 720],
		[41, 37, 810],
		[46, 41, 910],
		[51, 46, 1000],
		[61, 55, 1180],
		[71, 64, 1370],
		[81, 73, 1480],
		[91, 82, 1660],
		[101, 91, 1840],
		[121, 2320],
		[141, 128, 2680],
		[161, 146, 3100],
		[181, 164, 3630]
	]


		

