# Open the file and read its content.
raw_data = open('WFPVAM_FoodPrices_24-01-2017.csv', 'r').read()

# Split the raw_data on every newline.
raw_data = raw_data.split('\n')
# Take of the headers
raw_data_no_header = raw_data[1:]

# Make a list of lists of the raw_data_no_header
staples_data = []
for food_info in raw_data_no_header:
    info = food_info.split(',')
    staples_data.append(info)

def load_data_by_country(country):
    """Fetch data based on a specific country"""
    country_data = []
    for info in staples_data:
        if country in info:
            country_data.append(info)
    return(country_data)

# Fetch all data on Ghana
ghana_food_stapes = load_data_by_country(country="Ghana")

# Regions where there are market activities
regions = []
for info in ghana_food_stapes:
    current_region = info[3]
    if current_region not in regions:
        regions.append(current_region)

"""
Explanation: The Landscape of the Major Food Staples in Ghana
The dataset used in this project is a CSV file of the Global Food Prices Database by WFP - World Food Programme.
End of explanation
"""


for region in sorted(regions):
    print("{} Region".format(region))

# Market locations across the country
markets = []
for info in ghana_food_stapes:
    current_market = info[5]
    if current_market not in markets:
        markets.append(current_market)

# Markets per Regions
markets_in_regions = {}
for region in regions:
    current_market = []
    for market in markets:
        for info in ghana_food_stapes:
            if region == info[3] and market == info[5]:
                if market not in current_market:
                    current_market.append(market)
                markets_in_regions.update({region: current_market})


"""
Explanation: The regions where there are market activities - alphabetical order:
End of explanation
"""


# Loop through the markets_in_regions dictionary
for region, markets in sorted(markets_in_regions.items()):
    print("\n{} REGION".format(region.upper()))
    if len(markets) > 1:
        # If market more than 1 make location plural
        print(" {} Market Locations:".format(len(markets)))
    elif len(markets) == 1:
        # If only 1 market, make location singular
        print(" {} Market Location:".format(len(markets)))
    for market in sorted(markets):
        print("\t{}".format(market))

"""
Explanation: List of market locations per region
End of explanation
"""


staples = []
for info in ghana_food_stapes:
    current_staple = info[7]
    if current_staple not in staples:
        staples.append(current_staple)

for staple in sorted(staples):
    print(staple)

"""
Explanation: The main staples in Ghana
End of explanation
"""


