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

import '../../../constant/box_name.dart';
import '../../functions/digit_obsecur_formate.dart';
import '../../functions/secure_storage.dart';

class CreditCardController extends GetxController {
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final TextEditingController cardNumberController = TextEditingController();
  final TextEditingController cardHolderNameController =
      TextEditingController();
  final TextEditingController expiryDateController = TextEditingController();
  final TextEditingController cvvCodeController = TextEditingController();
  openPayment() async {
    String? cardNumber = await SecureStorage().readData(BoxName.cardNumber);
    String? cardHolderName =
        await SecureStorage().readData(BoxName.cardHolderName);
    String? expiryDate = await SecureStorage().readData(BoxName.expiryDate);
    String? cvvCode = await SecureStorage().readData(BoxName.cvvCode);

    print('cardNumber: $cardNumber');
    print('cardHolderName: $cardHolderName');
    print('expiryDate: $expiryDate');
    print('cvvCode: $cvvCode');

    if (cvvCode != null && cvvCode.isNotEmpty) {
      final maskedCardNumber = DigitObscuringFormatter()
          .formatEditUpdate(
            TextEditingValue.empty,
            TextEditingValue(text: cardNumber ?? ''),
          )
          .text;

      print('maskedCardNumber: $maskedCardNumber');

      cardNumberController.text = maskedCardNumber;
      cardHolderNameController.text = cardHolderName ?? '';
      expiryDateController.text = expiryDate ?? '';
      cvvCodeController.text = cvvCode;
    }
  }

  @override
  void onInit() async {
    super.onInit();
    openPayment();
    // String? cardNumber = await SecureStorage().readData(BoxName.cardNumber);
    // String? cardHolderName =
    //     await SecureStorage().readData(BoxName.cardHolderName);
    // String? expiryDate = await SecureStorage().readData(BoxName.expiryDate);
    // String? cvvCode = await SecureStorage().readData(BoxName.cvvCode);

    // print('cardNumber: $cardNumber');
    // print('cardHolderName: $cardHolderName');
    // print('expiryDate: $expiryDate');
    // print('cvvCode: $cvvCode');

    // if (cvvCode != null && cvvCode.isNotEmpty) {
    //   final maskedCardNumber = DigitObscuringFormatter()
    //       .formatEditUpdate(
    //         TextEditingValue.empty,
    //         TextEditingValue(text: cardNumber ?? ''),
    //       )
    //       .text;

    //   print('maskedCardNumber: $maskedCardNumber');

    //   cardNumberController.text = maskedCardNumber;
    //   cardHolderNameController.text = cardHolderName ?? '';
    //   expiryDateController.text = expiryDate ?? '';
    //   cvvCodeController.text = cvvCode;
    // }
  }
}

class CreditCardModel {
  String cardNumber;
  String cardHolderName;
  String expiryDate;
  String cvvCode;

  CreditCardModel({
    required this.cardNumber,
    required this.cardHolderName,
    required this.expiryDate,
    required this.cvvCode,
  });
}
