import 'dart:convert';

import 'package:firebase_database/firebase_database.dart';
import 'package:mobile_pos/model/transition_model.dart';

import '../currency.dart';
import '../model/due_transaction_model.dart';

class TransitionRepo {
  Future<List<SalesTransitionModel>> getAllTransition() async {
    List<SalesTransitionModel> transitionList = [];
    final transitionRef = FirebaseDatabase.instance.ref(constUserId).child('Sales Transition');

    await transitionRef.orderByKey().get().then((value) {
      for (var element in value.children) {
        SalesTransitionModel data =
            SalesTransitionModel.fromJson(jsonDecode(jsonEncode(element.value)));
        data.key = element.key;
        transitionList.add(data);
      }
    });

    transitionRef.keepSynced(true);
    return transitionList;
  }
}

class PurchaseTransitionRepo {
  Future<List<dynamic>> getAllTransition() async {
    List<dynamic> transitionList = [];
    final purchaseTransitionRef =
        FirebaseDatabase.instance.ref(constUserId).child('Purchase Transition');
    await purchaseTransitionRef.orderByKey().get().then((value) {
      for (var element in value.children) {
        PurchaseTransactionModel data =
            PurchaseTransactionModel.fromJson(jsonDecode(jsonEncode(element.value)));
        data.key = element.key;
        transitionList.add(data);
      }
    });

    purchaseTransitionRef.keepSynced(true);
    return transitionList;
  }
}

class DueTransitionRepo {
  Future<List<DueTransactionModel>> getAllTransition() async {
    List<DueTransactionModel> transitionList = [];
    final dueTransitionRef = FirebaseDatabase.instance.ref(constUserId).child('Due Transaction');
    await dueTransitionRef.orderByKey().get().then((value) {
      for (var element in value.children) {
        transitionList.add(DueTransactionModel.fromJson(jsonDecode(jsonEncode(element.value))));
      }
    });

    dueTransitionRef.keepSynced(true);
    return transitionList;
  }
}

class QuotationRepo {
  Future<List<SalesTransitionModel>> getAllQuotation() async {
    List<SalesTransitionModel> transitionList = [];
    final quotationRef = FirebaseDatabase.instance.ref(constUserId).child('Sales Quotation');
    await quotationRef.orderByKey().get().then((value) {
      for (var element in value.children) {
        transitionList.add(SalesTransitionModel.fromJson(jsonDecode(jsonEncode(element.value))));
      }
    });
    quotationRef.keepSynced(true);
    return transitionList;
  }
}

class QuotationHistoryRepo {
  Future<List<SalesTransitionModel>> getAllQuotationHistory() async {
    List<SalesTransitionModel> transitionList = [];
    final quotationConvertRef =
        FirebaseDatabase.instance.ref(constUserId).child('Quotation Convert History');
    await quotationConvertRef.orderByKey().get().then((value) {
      for (var element in value.children) {
        transitionList.add(SalesTransitionModel.fromJson(jsonDecode(jsonEncode(element.value))));
      }
    });
    quotationConvertRef.keepSynced(true);
    return transitionList;
  }
}
