--MjTs-140914
--PTE 6.5.3 and iPatch v3
local inifile = ".\\modules\\ms_settings.ini"

local switch_map_pte_c = {
    [0] = 33,	 -- 0:  Exhibition
    [154] = 33,  -- 154:  KONAMI CUP
    [155] = 33,  -- 155:  KONAMI CUP
    [84] = 33,   -- 84:  The Emirates FA CUP
    [101] = 33,  -- 101: The FA Community Shield
    [39] = 33,   -- 39:  Premier League
    [43] = 33,   -- 43: Laliga
    [85] = 33,   -- 85: Copa Del Rey
    [51] = 33,   -- 51: Ligue 1
    [47] = 33,   -- 47: Serie A
    [72] = 33,   -- 72: Bundesliga
    [86] = 33,   -- 86: TIM CUP
    [138] = 33,  -- 138: EURO
    [139] = 33,  -- 139: EURO
    [145] = 33,  -- 145: CACD
    [44] = 33,   -- 44: Laliga 123
    [48] = 33,   -- 48: Serie Bkt
	[40] = 33,   -- SkyBET Championship
}
local switch_map_pte_smb = {
    [0] = 23,	 -- 0:  Exhibition
    [154] = 24,  -- 154:  KONAMI CUP
    [155] = 24,  -- 155:  KONAMI CUP
    [84] = 33,   -- 84:  The Emirates FA CUP
    [101] = 33,  -- 101: The FA Community Shield
    [39] = 33,   -- 39:  Premier League
    [43] = 33,   -- 43: Laliga
    [85] = 33,   -- 85: Copa Del Rey
    [51] = 33,   -- 51: Ligue 1
    [47] = 33,   -- 47: Serie A
    [72] = 33,   -- 72: Bundesliga
    [86] = 33,   -- 86: TIM CUP
    [138] = 33,  -- 138: EURO
    [139] = 33,  -- 139: EURO
    [145] = 33,  -- 145: CACD
    [44] = 33,   -- 44: Laliga 123
    [48] = 33,   -- 48: Serie Bkt
	[40] = 33,   -- SkyBET Championship
}
local switch_map_pte_d = {
    [84] = 33,   -- 84:  The Emirates FA CUP
    [101] = 33,  -- 101: The FA Community Shield
    [39] = 33,   -- 39:  Premier League
    [43] = 33,   -- 43: Laliga
    [85] = 33,   -- 85: Copa Del Rey
    [51] = 33,   -- 51: Ligue 1
    [47] = 33,   -- 47: Serie A
    [72] = 33,   -- 72: Bundesliga
    [86] = 33,   -- 86: TIM CUP
    [138] = 33,  -- 138: EURO
    [139] = 33,  -- 139: EURO
    [145] = 33,  -- 145: CACD
    [44] = 33,   -- 44: Laliga 123
    [48] = 33,   -- 48: Serie Bkt
	[40] = 33,   -- SkyBET Championship
}


local switch_map_ipatch_c = {
    [0] = 33,	 -- 0:  Exhibition
    [154] = 33,  -- 154:  KONAMI CUP
    [155] = 33,  -- 155:  KONAMI CUP
    [84] = 33,   -- 84:  The Emirates FA CUP
    [101] = 33,  -- 101: The FA Community Shield
    [39] = 33,   -- 39:  Premier League
    [43] = 33,   -- 43: Laliga
    [85] = 33,   -- 85: Copa Del Rey
    [51] = 33,   -- 51: Ligue 1
    [47] = 33,   -- 47: Serie A
    [72] = 33,   -- 72: Bundesliga
    [57] = 33,   -- 57: Gojek Traveloka Liga 1
    [86] = 33,   -- 86: TIM CUP
    [138] = 33,   -- 138: EURO
    [139] = 33,   -- 139: EURO
    [145] = 33,   -- 145: CACD
    [44] = 33,   -- 44: Laliga 123
    [48] = 33,   -- 48: Serie Bkt
	[40] = 33,   -- SkyBET Championship
}
local switch_map_ipatch_smb = {
    [0] = 23,	 -- 0:  Exhibition
    [154] = 24,  -- 154:  KONAMI CUP
    [155] = 24,  -- 155:  KONAMI CUP
    [84] = 33,   -- 84:  The Emirates FA CUP
    [101] = 33,  -- 101: The FA Community Shield
    [39] = 33,   -- 39:  Premier League
    [43] = 33,   -- 43: Laliga
    [85] = 33,   -- 85: Copa Del Rey
    [51] = 33,   -- 51: Ligue 1
    [47] = 33,   -- 47: Serie A
    [86] = 33,   -- 86: TIM CUP
    [72] = 33,   -- 72: Bundesliga
    [57] = 33,   -- 57: Gojek Traveloka Liga 1
    [138] = 33,   -- 138: EURO
    [139] = 33,   -- 139: EURO
    [145] = 33,   -- 145: CACD
    [44] = 33,   -- 44: Laliga 123
    [48] = 33,   -- 48: Serie Bkt
	[40] = 33,   -- SkyBET Championship
}
local switch_map_ipatch_d = {
    [84] = 33,   -- 84:  The Emirates FA CUP
    [101] = 33,  -- 101: The FA Community Shield
    [39] = 33,   -- 39:  Premier League
    [43] = 33,   -- 43: Laliga
    [85] = 33,   -- 85: Copa Del Rey
    [51] = 33,   -- 51: Ligue 1
    [47] = 33,   -- 47: Serie A
    [86] = 33,   -- 86: TIM CUP
    [72] = 33,   -- 72: Bundesliga
    [57] = 33,   -- 57: Gojek Traveloka Liga 1
    [138] = 33,   -- 138: EURO
    [139] = 33,   -- 139: EURO
    [145] = 33,   -- 145: CACD
    [44] = 33,   -- 44: Laliga 123
    [48] = 33,   -- 48: Serie Bkt
	[40] = 33,   -- SkyBET Championship
}

function read_ini(ctx)
    if inifile:sub(1,1) == "." then
        inifile = ctx.sider_dir .. inifile
    end
    local t = {}
    for line in io.lines(inifile) do
        local name, value = string.match(line, "^(selected.patch)%s*=%s*([-%d.]+)")
        if name and value then
            t[name] = tonumber(value) or value
			patch_select = t[name]
        end
        local name_c, value_c = string.match(line, "^(selected.scene)%s*=%s*([-%d.]+)")
        if name_c and value_c then
            t[name_c] = tonumber(value_c) or value_c
			scene_select = t[name_c]
        end
    end
    return t
end	
-- current (actual) tournament id
-- nil, if we do not have it in switch_map
local tid
local function each_match(ctx)
	read_ini(ctx)
end
local function switch_tournament(ctx, tournament_id)
	if patch_select == 0 and scene_select == 0 then
	   log("selected patch is: PTE 6.5.3")	
	   log("selected scene exhibition is: Default")		   
       rep_id = switch_map_pte_d[tournament_id]
	elseif patch_select == 0 and scene_select == 1 then
	   log("selected patch is: PTE 6.5.3")	
	   log("selected scene exhibition is: Cinematics")		   
       rep_id = switch_map_pte_c[tournament_id]
	elseif patch_select == 0 and scene_select == 2 then
	   log("selected patch is: PTE 6.5.3")	
	   log("selected scene exhibition is: Smoke Bomb")		   
       rep_id = switch_map_pte_smb[tournament_id]
	elseif patch_select == 1 and scene_select == 0 then
	   log("selected patch is: iPatch v3")	
	   log("selected scene exhibition is: Default")		   
       rep_id = switch_map_ipatch_d[tournament_id]
	elseif patch_select == 1 and scene_select == 1 then
	   log("selected patch is: iPatch v3")	
	   log("selected scene exhibition is: Cinematics")		   
       rep_id = switch_map_ipatch_c[tournament_id]
	elseif patch_select == 1 and scene_select == 2 then
	   log("selected patch is: iPatch v3")	
	   log("selected scene exhibition is: Smoke Bomb")		   
       rep_id = switch_map_ipatch_smb[tournament_id]
	end
    tid = rep_id and tournament_id or nil
	home = ctx.home_team
    if rep_id then
        log(string.format(
            "switching tournament_id: %d --> %d", tournament_id, rep_id))
    else
        -- do not switch, but log the tournament id so that
        -- we can add this information to tournaments.txt and
        -- eventually have a complete list of ids
        log(string.format("tournament_id: %d", tournament_id))
    end
	--Disable Cinematic or Smokebomb for National Team
    if patch_select == 0 then
		if home == 1165 or home == 21 or home == 9 or home == 1170 or home == 27 or home == 24 or home == 13 or home == 15 or home == 5 or home == 8 or home == 14 or home == 28 or home == 22 or home == 1175 or home == 1 or home == 1164 or home == 12 or home == 10 or home == 2 or home == 16 or home == 19 or home == 6 or home == 26 or home == 31 or home == 3 or home == 1550 or home == 20 or home == 23 or home == 7 or home == 17 or home == 11 or home == 29 or home == 30 or home == 4 or home == 1040 or home == 1044 or home == 36 or home == 1051 or home == 34 or home == 1058 or home == 1059 or home == 1067 or home == 32 or home == 35 or home == 38 or home == 37 or home == 33 or home == 1083 or home == 42 or home == 43 or home == 41 or home == 40 or home == 1113 or home == 39 or home == 50 or home == 1128 or home == 45 or home == 47 or home == 44 or home == 51 or home == 48 or home == 46 or home == 49 or home == 1129 or home == 57 or home == 54 or home == 55 or home == 4025 or home == 1010 or home == 52 or home == 1011 or home == 1012 or home == 1013 or home == 1015 or home == 4020 or home == 1022 or home == 1026 or home == 53 or home == 56 or home == 1031 or home == 1032 or home == 1184 or home == 1141 or home == 4021 or home == 91420 or home == 107304 or home == 107307 or home == 107306 or home == 107305 or home == 2703 or home == 2704 then
			return nil
		else
			return rep_id or tournament_id
		end
    elseif patch_select == 1 then
		if home == 1165 or home == 5066 or home == 21 or home == 5568 or home == 9 or home == 1170 or home == 27 or home == 24 or home == 13 or home == 15 or home == 5 or home == 8 or home == 14 or home == 28 or home == 22 or home == 1175 or home == 1 or home == 1164 or home == 12 or home == 2510 or home == 5068 or home == 10 or home == 2 or home == 16 or home == 19 or home == 6 or home == 26 or home == 31 or home == 3 or home == 1550 or home == 23 or home == 20 or home == 7 or home == 17 or home == 11 or home == 29 or home == 30 or home == 4 or home == 1040 or home == 1044 or home == 36 or home == 1051 or home == 1050 or home == 34 or home == 1056 or home == 1058 or home == 1059 or home == 1060 or home == 1067 or home == 32 or home == 35 or home == 38 or home == 37 or home == 1081 or home == 33 or home == 1082 or home == 1083 or home == 1084 or home == 42 or home == 43 or home == 41 or home == 40 or home == 1113 or home == 1114 or home == 39 or home == 50 or home == 1128 or home == 45 or home == 47 or home == 44 or home == 51 or home == 48 or home == 46 or home == 49 or home == 1129 or home == 57 or home == 1003 or home == 1004 or home == 1005 or home == 1006 or home == 54 or home == 1030 or home == 1007 or home == 1008 or home == 1009 or home == 55 or home == 1010 or home == 52 or home == 1011 or home == 1012 or home == 53 or home == 1013 or home == 5067 or home == 1014 or home == 1015 or home == 1017 or home == 1019 or home == 1020 or home == 1022 or home == 1024 or home == 1025 or home == 1026 or home == 56 or home == 1027 or home == 1023 or home == 5069 or home == 1031 or home == 1039 or home == 5060 or home == 1032 or home == 1184 or home == 1033 or home == 1139 or home == 1141 or home == 1142 then
			return nil
		else
			return rep_id or tournament_id
		end
    end
end


local function init(ctx)
    ctx.register("set_home_team", each_match)
    ctx.register("tournament_check_for_trophy", switch_tournament)
end

return { init = init }
