import 'dart:convert';
import 'package:flutter/services.dart';

class RandomPickGrid {
  RandomPickGrid();

  String _grid;

  init(String difficulty, String size) async {
    _grid = '';
    await gridFromLocalFile(difficulty, size);
  }

  Future<void> gridFromLocalFile(String difficulty, String size) async {
    // Get global grids list
    List grids;
    try {
      String jsonString = await rootBundle.loadString('assets/files/templates.json');
      final jsonResponse = await json.decode(jsonString);
      grids = jsonResponse['templates'][size][difficulty];
    } catch (e) {
      print("$e");
    }

    // Check we have enough grids
    if (grids.length < 1) {
      print('Not enough grids [' + size + ', ' + difficulty + '] in templates.');
    }

    // Randomize grids list
    grids.shuffle();

    // Pick first grids from shuffled list
    _grid = grids.take(1).toList()[0];
  }

  String get grid => _grid;
}