// load json array from https://github.com/FriendlyUser/flutter_pokedex/blob/main/scripts/clean_pokemon.json
// load json array from https://raw.githubusercontent.com/FriendlyUser/flutter_pokedex/main/scripts/clean_pokemon.json
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
public class PokemonV2Pokemontype
{
    public PokemonV2Type pokemon_v2_type { get; set; }
}
public class PokemonV2Type
{
    public string name { get; set; }
    // public int move_damage_class_id { get; set; }
    public int generation_id { get; set; }
    public int id { get; set; }
}
public class Pokemon
{
    public string name { get; set; }
    public int id { get; set; }
    public int pokemon_species_id { get; set; }
    public List<PokemonV2Pokemontype> pokemon_v2_pokemontypes { get; set; }
    public string img_src { get; set; }
}
public class PokemonList
{
    public List<Pokemon> pokemon { get; set; }
}
// load json array from https://raw.githubusercontent.com/FriendlyUser/flutter_pokedex/main/scripts/clean_pokemon.json
var client = new HttpClient();
var response = client.GetAsync("https://raw.githubusercontent.com/FriendlyUser/flutter_pokedex/main/scripts/clean_pokemon.json").Result;
var content = response.Content.ReadAsStringAsync().Result;
// parse content into a list of Pokemon objects
List<Pokemon> pokemonList = 
                JsonSerializer.Deserialize<List<Pokemon>>(content);
// print out the first 10 pokemon
for (int i = 0; i < 10; i++)
{
    Console.WriteLine(pokemonList[i].name);
}
bulbasaur ivysaur venusaur charmander charmeleon charizard squirtle wartortle blastoise caterpie
#i "nuget:https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet7/nuget/v3/index.json" 
#i "nuget:https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" 
#r "nuget: Plotly.NET, 3.0.1"
#r "nuget: Plotly.NET.Interactive, 3.0.2"
Loading extensions from `/home/codespace/.nuget/packages/plotly.net.interactive/3.0.2/interactive-extensions/dotnet/Plotly.NET.Interactive.dll`
using System.Linq;
using Microsoft.FSharp.Core;
using Plotly.NET;
using static Plotly.NET.GenericChartExtensions;
using Plotly.NET.LayoutObjects;
// group the pokemon by type, can have duplicates
var groupedPokemon = new Dictionary<string, int>();
foreach (var pokemon in pokemonList)
{
    foreach (var type in pokemon.pokemon_v2_pokemontypes)
    {
        if (groupedPokemon.ContainsKey(type.pokemon_v2_type.name))
        {
            groupedPokemon[type.pokemon_v2_type.name] = groupedPokemon[type.pokemon_v2_type.name] + 1;
        }
        else
        {
            groupedPokemon.Add(type.pokemon_v2_type.name, 1);
        }
    }
}
void makePlot(Dictionary<string, int> dataDict, string xTitle, string yTitle, string title, int width = 1280) 
{
    var x = dataDict.Keys.ToArray();
    var y = dataDict.Values.ToArray();
    LinearAxis xAxis = new LinearAxis();
    xAxis.SetValue("title", xTitle);
    xAxis.SetValue("showgrid", false);
    xAxis.SetValue("showline", true);
    LinearAxis yAxis = new LinearAxis();
    yAxis.SetValue("title", yTitle);
    yAxis.SetValue("showgrid", false);
    yAxis.SetValue("showline", true);
    Layout layout = new Layout();
    layout.SetValue("xaxis", xAxis);
    layout.SetValue("yaxis", yAxis);
    layout.SetValue("title", title);
    layout.SetValue("width", width);
    Trace trace = new Trace("bar");
    trace.SetValue("x", x);
    trace.SetValue("y", y);
    trace.SetValue("mode", "markers");
    // trace.SetValue("name", characterName);
    var chart = GenericChart.ofTraceObject(true, trace).WithLayout(layout);
    // figure out how to save static files
    var data = GenericChart.toChartHTML(chart);
    DisplayExtensions.DisplayAs(data,"text/html");
}
makePlot(groupedPokemon, "Type", "Number", "Number of pokemon per type (up to generation viii", 1280);
// number of pokemon per generation
var groupedPokemonByGeneration = new Dictionary<string, int>();
foreach (var pokemon in pokemonList)
{
    if (groupedPokemonByGeneration.ContainsKey(pokemon.pokemon_v2_pokemontypes.First().pokemon_v2_type.generation_id.ToString()))
    {
        groupedPokemonByGeneration[pokemon.pokemon_v2_pokemontypes.First().pokemon_v2_type.generation_id.ToString()] = groupedPokemonByGeneration[pokemon.pokemon_v2_pokemontypes.First().pokemon_v2_type.generation_id.ToString()] + 1;
    }
    else
    {
        groupedPokemonByGeneration.Add(pokemon.pokemon_v2_pokemontypes.First().pokemon_v2_type.generation_id.ToString(), 1);
    }
}
makePlot(groupedPokemonByGeneration, "Generation", "Number", "Number of types by generation (up to generation viii)", 1280);
// as you can see new types were adding in gen 6 and gen 2
// starting letters for pokemon plot
var groupedPokemonByFirstLetter = new Dictionary<string, int>();
foreach (var pokemon in pokemonList)
{
    var firstLetter = pokemon.name.Substring(0, 1);
    if (groupedPokemonByFirstLetter.ContainsKey(firstLetter))
    {
        groupedPokemonByFirstLetter[firstLetter]++;
    }
    else
    {
        groupedPokemonByFirstLetter.Add(firstLetter, 1);
    }
}
// plot 
makePlot(groupedPokemonByFirstLetter, "First Letter", "Number", "Number of pokemon by first letter", 1280);