defmodule WebApp.CityProducer do
  @moduledoc false

  use GenStage

  require Logger

  def start_link(_args) do
    GenStage.start_link(__MODULE__, [], name: __MODULE__)
  end

  def init(_args) do
    {:producer, city_list()}
  end

  def handle_demand(demand, state) when demand > length(state) do
    handle_demand(demand, state ++ city_list())
  end

  def handle_demand(demand, state) do
    {to_dispatch, remaining} = Enum.split(state, demand)

    {:noreply, to_dispatch, remaining}
  end

  defp city_list do
    [
      {"Abu Dhabi", "United Arab Emirates"},
      {"Abuja", "Nigeria"},
      {"Accra", "Ghana"},
      {"Adamstown", "Pitcairn Islands"},
      {"Addis Ababa", "Ethiopia"},
      {"Algiers", "Algeria"},
      {"Alofi", "Niue"},
      {"Amman", "Jordan"},
      {"Amsterdam", "Netherlands"},
      {"Andorra la Vella", "Andorra"},
      {"Ankara", "Turkey"},
      {"Antananarivo", "Madagascar"},
      {"Apia", "Samoa"},
      {"Ashgabat", "Turkmenistan"},
      {"Asmara", "Eritrea"},
      {"Asunción", "Paraguay"},
      {"Athens", "Greece"},
      {"Avarua", "Cook Islands"},
      {"Baghdad", "Iraq"},
      {"Baku", "Azerbaijan"},
      {"Bamako", "Mali"},
      {"Bandar Seri Begawan", "Brunei"},
      {"Bangkok", "Thailand"},
      {"Bangui", "Central African Republic"},
      {"Banjul", "Gambia"},
      {"Basseterre", "Saint Kitts and Nevis"},
      {"Beijing", "China"},
      {"Beirut", "Lebanon"},
      {"Belgrade", "Serbia"},
      {"Belmopan", "Belize"},
      {"Berlin", "Germany"},
      {"Bern", "Switzerland"},
      {"Bishkek", "Kyrgyzstan"},
      {"Bissau", "Guinea-Bissau"},
      {"Bogotá", "Colombia"},
      {"Brasília", "Brazil"},
      {"Bratislava", "Slovakia"},
      {"Brazzaville", "Republic of the Congo"},
      {"Bridgetown", "Barbados"},
      {"Brussels", "Belgium"},
      {"Bucharest", "Romania"},
      {"Budapest", "Hungary"},
      {"Buenos Aires", "Argentina"},
      {"Cairo", "Egypt"},
      {"Canberra", "Australia"},
      {"Caracas", "Venezuela"},
      {"Castries", "Saint Lucia"},
      {"Chișinău", "Moldova"},
      {"Cockburn Town", "Turks and Caicos Islands"},
      {"Conakry", "Guinea"},
      {"Copenhagen", "Denmark"},
      {"Dakar", "Senegal"},
      {"Damascus", "Syria"},
      {"Dhaka", "Bangladesh"},
      {"Dili", "East Timor"},
      {"Djibouti", "Djibouti"},
      {"Dodoma", "Tanzania"},
      {"Doha", "Qatar"},
      {"Douglas", "Isle of Man"},
      {"Dublin", "Ireland"},
      {"Dushanbe", "Tajikistan"},
      {"Flying Fish Cove", "Christmas Island"},
      {"Freetown", "Sierra Leone"},
      {"Funafuti", "Tuvalu"},
      {"Gaborone", "Botswana"},
      {"George Town", "Cayman Islands"},
      {"Georgetown", "Ascension Island"},
      {"Georgetown", "Guyana"},
      {"Gibraltar", "Gibraltar"},
      {"Gitega", "Burundi"},
      {"Guatemala City", "Guatemala"},
      {"Hagatna", "Guam"},
      {"Hamilton", "Bermuda"},
      {"Hanoi", "Vietnam"},
      {"Harare", "Zimbabwe"},
      {"Havana", "Cuba"},
      {"Helsinki", "Finland"},
      {"Honiara", "Solomon Islands"},
      {"Islamabad", "Pakistan"},
      {"Jakarta", "Indonesia"},
      {"Jerusalem", "Israel"},
      {"Juba", "South Sudan"},
      {"Kabul", "Afghanistan"},
      {"Kampala", "Uganda"},
      {"Kathmandu", "Nepal"},
      {"Khartoum", "Sudan"},
      {"Kiev", "Ukraine"},
      {"Kigali", "Rwanda"},
      {"Kingston", "Jamaica"},
      {"Kingston", "Norfolk Island"},
      {"Kingstown", "Saint Vincent and the Grenadines"},
      {"Kinshasa", "Democratic Republic of the Congo"},
      {"Kuala Lumpur", "Malaysia"},
      {"Kuwait City", "Kuwait"},
      {"Libreville", "Gabon"},
      {"Lilongwe", "Malawi"},
      {"Lima", "Peru"},
      {"Lisbon", "Portugal"},
      {"Ljubljana", "Slovenia"},
      {"Lomé", "Togo"},
      {"London", "United Kingdom"},
      {"Luanda", "Angola"},
      {"Lusaka", "Zambia"},
      {"Luxembourg", "Luxembourg"},
      {"Madrid", "Spain"},
      {"Majuro", "Marshall Islands"},
      {"Malabo", "Equatorial Guinea"},
      {"Malé", "Maldives"},
      {"Managua", "Nicaragua"},
      {"Manama", "Bahrain"},
      {"Manila", "Philippines"},
      {"Maputo", "Mozambique"},
      {"Marigot", "Saint Martin"},
      {"Maseru", "Lesotho"},
      {"Mata-Utu", "Wallis and Futuna"},
      {"Mbabane", "Eswatini"},
      {"Mexico City", "Mexico"},
      {"Minsk", "Belarus"},
      {"Mogadishu", "Somalia"},
      {"Monaco", "Monaco"},
      {"Monrovia", "Liberia"},
      {"Montevideo", "Uruguay"},
      {"Moroni", "Comoros"},
      {"Moscow", "Russia"},
      {"Muscat", "Oman"},
      {"Nairobi", "Kenya"},
      {"Nassau", "Bahamas"},
      {"Naypyidaw", "Myanmar"},
      {"N'Djamena", "Chad"},
      {"New Delhi", "India"},
      {"Niamey", "Niger"},
      {"Nouakchott", "Mauritania"},
      {"Nouméa", "New Caledonia"},
      {"Nuuk", "Greenland"},
      {"Oranjestad", "Aruba"},
      {"Oslo", "Norway"},
      {"Ottawa", "Canada"},
      {"Ouagadougou", "Burkina Faso"},
      {"Pago Pago", "American Samoa"},
      {"Panama City", "Panama"},
      {"Papeete", "French Polynesia"},
      {"Paramaribo", "Suriname"},
      {"Paris", "France"},
      {"Philipsburg", "Sint Maarten"},
      {"Phnom Penh", "Cambodia"},
      {"Plymouth", "Montserrat"},
      {"Podgorica", "Montenegro"},
      {"Port Louis", "Mauritius"},
      {"Port Moresby", "Papua New Guinea"},
      {"Port-au-Prince", "Haiti"},
      {"Port of Spain", "Trinidad and Tobago"},
      {"Porto-Novo", "Benin"},
      {"Prague", "Czech Republic"},
      {"Praia", "Cape Verde"},
      {"Pretoria", "South Africa"},
      {"Pyongyang", "North Korea"},
      {"Quito", "Ecuador"},
      {"Rabat", "Morocco"},
      {"Ramallah", "Palestine"},
      {"Reykjavík", "Iceland"},
      {"Riga", "Latvia"},
      {"Riyadh", "Saudi Arabia"},
      {"Rome", "Italy"},
      {"Roseau", "Dominica"},
      {"Saipan", "Northern Mariana Islands"},
      {"San José", "Costa Rica"},
      {"San Juan", "Puerto Rico"},
      {"San Marino", "San Marino"},
      {"San Salvador", "El Salvador"},
      {"Sana'a", "Yemen"},
      {"Santiago", "Chile"},
      {"Santo Domingo", "Dominican Republic"},
      {"São Tomé", "São Tomé and Príncipe"},
      {"Seoul", "South Korea"},
      {"Singapore", "Singapore"},
      {"Skopje", "North Macedonia"},
      {"Sofia", "Bulgaria"},
      {"St. George's", "Grenada"},
      {"St. Pierre", "Saint Pierre and Miquelon"},
      {"Stanley", "Falkland Islands"},
      {"Stepanakert", "Artsakh"},
      {"Stockholm", "Sweden"},
      {"Sucre", "Bolivia"},
      {"Suva", "Fiji"},
      {"Taipei", "Taiwan"},
      {"Tallinn", "Estonia"},
      {"Tamar", "Hong Kong"},
      {"Tarawa", "Kiribati"},
      {"Tashkent", "Uzbekistan"},
      {"Tbilisi", "Georgia"},
      {"Tegucigalpa", "Honduras"},
      {"Tehran", "Iran"},
      {"Thimphu", "Bhutan"},
      {"Tirana", "Albania"},
      {"Tiraspol", "Transnistria"},
      {"Tokyo", "Japan"},
      {"Tripoli", "Libya"},
      {"Tunis", "Tunisia"},
      {"Ulaanbaatar", "Mongolia"},
      {"Vaduz", "Liechtenstein"},
      {"Valletta", "Malta"},
      {"The Valley", "Anguilla"},
      {"Victoria", "Seychelles"},
      {"Vienna", "Austria"},
      {"Vientiane", "Laos"},
      {"Vilnius", "Lithuania"},
      {"Warsaw", "Poland"},
      {"Washington, D.C.", "United States"},
      {"Wellington", "New Zealand"},
      {"West Island", "Cocos (Keeling) Islands"},
      {"Willemstad", "Curaçao"},
      {"Windhoek", "Namibia"},
      {"Yamoussoukro", "Ivory Coast"},
      {"Yaoundé", "Cameroon"},
      {"Yaren", "Nauru"},
      {"Yerevan", "Armenia"},
      {"Zagreb", "Croatia"}
    ]
  end
end
