import 'dart:convert';

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

import '../../Helper/Constants/Colorconstants.dart';
import '../../Helper/Logger.dart';

class HomeController extends GetxController{

  int selectedIndex = 0;
  int tenantId = 0;
  int locationId = 0;


  void onItemTapped(int index) {
    selectedIndex = index;
    update();
  }

  void getTenantId() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    tenantId = (await prefs.getInt('tenantId')) ?? 0;
    locationId = (await prefs.getInt('locationId')) ?? 0;
    logger.i('TenantId = ${tenantId}');
    update();
  }


  void showQRBottomSheet() {
    final Map<String, dynamic> qrMap = {
      'tenantid': tenantId,   // 👈 lowercase key
      'locationid': locationId, // 👈 lowercase key
    };

    final String qrData = jsonEncode(qrMap);
    logger.i("QR JSON Data: $qrData");

    final qrCode = QrCode.fromData(
      data: qrData,
      errorCorrectLevel: QrErrorCorrectLevel.M,
    );

    Get.bottomSheet(
      SafeArea(
        child: Container(
          padding: const EdgeInsets.all(24),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: const BorderRadius.vertical(top: Radius.circular(24)),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.1),
                blurRadius: 10,
                offset: const Offset(0, -2),
              ),
            ],
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Text(
                "Scan QR Code",
                style: TextStyle(
                  fontSize: 22,
                  fontWeight: FontWeight.bold,
                  color: Colors.black87,
                ),
              ),
              const SizedBox(height: 24),
              Center(
                child: Container(
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(16),
                    border: Border.all(color: Colors.grey.shade200, width: 1),
                  ),
                  child: SizedBox(
                    width: 220,
                    height: 220,
                    child: PrettyQrView(
                      qrImage: QrImage(qrCode),
                      decoration: const PrettyQrDecoration(
                        shape: PrettyQrSmoothSymbol(
                          roundFactor: 0.0,
                          color: Colors.black,
                        ),
                        quietZone: PrettyQrModulesQuietZone(2),
                        background: Colors.white,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
    );
  }




  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    getTenantId();
  }
}