import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../app.dart';
import 'state.dart';

class HomeController extends GetxController {
  final state = HomeState();

  @override
  void onInit() {
    debugPrint('HomeController: onInit()');
    getSliders();
    super.onInit();
  }

  Future<void> getSliders() async {
    state.isLoading.value = true;

    state.sliders.clear();
    state.banners1.clear();
    state.banners2.clear();

    final resultSliders = await SliderApi.get();

    if (resultSliders.length == 0) {
      state.isLoading.value = false;
      return;
    }

    final List<SliderModel> tmpList = [];

    for (var i = 0; i < resultSliders.length; i++) {
      if (resultSliders[i].type == 'slider') {
        state.sliders.add(resultSliders[i]);
      } else {
        tmpList.add(resultSliders[i]);
      }
    }

    for (var i = 0; i < tmpList.length; i++) {
      if (i % 2 == 0)
        state.banners1.add(tmpList[i]);
      else
        state.banners2.add(tmpList[i]);
    }

    state.isLoading.value = false;
  }
}
