mirrored_repositories/yeezyid_bygetx/lib/app
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:image_picker/image_picker.dart';
import 'package:get/get.dart';
import 'package:tflite/tflite.dart';

class Controller extends GetxController {
  final ImagePicker _picker = ImagePicker();
  late final theImage = XFile("").path.obs;
  RxString type = ''.obs;
  RxString output = ''.obs;
  RxString url = 'https://findicons.com/files/icons/1008/quiet/256/no.png'.obs;
  RxString imageURL =
      'https://findicons.com/files/icons/1008/quiet/256/no.png'.obs;
  RxString highestBid = 'Highest Bid'.obs;
  RxString lowestAsk = 'Lowest Ask'.obs;
  RxString lastSale = 'Last Sale'.obs;

  ///加載型號模型
  loadModel000() async {
    await Tflite.loadModel(
        model: "assets/models/quantized_000.tflite",
        labels: "assets/models/labels000.txt");
    print("=================000模型加載成功===================");
    runModelOnImage();
  }

  ///加載配色模型
  loadModelother(String id) async {
    await Tflite.loadModel(
        model: "assets/models/quantized_" + id + ".tflite",
        labels: "assets/models/labels" + id + ".txt");
    print("=================" + id + "模型加載成功===================");
    runModelOnImage();
  }

  ///辨識
  runModelOnImage() async {
    var slist = [
      '350',
      '350v2',
      '380',
      '500',
      '700',
      '700MNVN',
      '700v2',
      '700v3',
      '750',
      '950',
      'Foam',
      'QNTM'
    ];
    var res = await Tflite.runModelOnImage(
      path: theImage.value,
      imageMean: 0.0,
      // defaults to 117.0
      imageStd: 255.0,
      // defaults to 1.0
      numResults: 2,
      // defaults to 5
      threshold: 0.2,
    );

    if (slist.contains(res![0]['label'])) {
      type.value = res[0]['label'];
      print("=================得到型號了 接下來加載配色模型===================");
      loadModelother(type.value);
    } else {
      output.value = res[0]['label'];
      print("=================得到配色了 接下來雲端取資料===================");
      getData();
    }
  }

  ///使用相機
  openCamera() async {
    var pathC = (await _picker.pickImage(source: ImageSource.camera));
    theImage.value = pathC!.path;
  }

  ///使用相冊
  openGallery() async {
    var pathG = (await _picker.pickImage(source: ImageSource.gallery));
    theImage.value = pathG!.path;
  }

  getData() async {
    await Firebase.initializeApp();
    var data = await FirebaseFirestore.instance
        .collection(type.value)
        .doc(output.value)
        .get();

    print('=================從FireStore拿到資料===================');

    url.value = data.data()!['URL'];
    imageURL.value = data.data()!['IMAGE URL'];
    lowestAsk.value = data.data()!['Lowest Ask'];
    lastSale.value = data.data()!['Last Sale'];
    highestBid.value = data.data()!['Highest Bid'];
  }
}
