import 'package:enaklo_pos/core/components/components.dart';
import 'package:enaklo_pos/core/extensions/build_context_ext.dart';
import 'package:enaklo_pos/presentation/setting/widgets/settings_title.dart';
import 'package:flutter/material.dart';
import 'package:enaklo_pos/core/constants/colors.dart';
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';

class ServerKeyPage extends StatefulWidget {
  final bool isTablet;
  const ServerKeyPage({
    super.key,
    this.isTablet = false,
  });

  @override
  State<ServerKeyPage> createState() => _ServerKeyPageState();
}

class _ServerKeyPageState extends State<ServerKeyPage> {
  TextEditingController? serverKeyController;

  String serverKey = '';

  Future<void> getServerKey() async {
    serverKey = await AuthLocalDataSource().getMitransServerKey();
  }

  @override
  void initState() {
    super.initState();
    serverKeyController = TextEditingController();
    getServerKey();
    //delay 2 detik
    Future.delayed(const Duration(seconds: 2), () {
      serverKeyController!.text = serverKey;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.background,
      body: Column(
        children: [
          SettingsTitle('Server Key'),
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              children: [
                Container(
                  padding: const EdgeInsets.all(20.0),
                  decoration: BoxDecoration(
                    color: AppColors.white,
                    borderRadius: BorderRadius.circular(8.0),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        'Server Key',
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                          color: AppColors.black,
                        ),
                      ),
                      SpaceHeight(4),
                      TextField(
                        controller: serverKeyController,
                        decoration: const InputDecoration(
                          border: OutlineInputBorder(),
                          hintText: 'Server Key',
                        ),
                      ),
                    ],
                  ),
                ),
                const SpaceHeight(20),
                Align(
                  alignment: Alignment.centerRight,
                  child: Button.filled(
                    width: context.deviceWidth * 0.2,
                    height: 44,
                    onPressed: () {
                      AuthLocalDataSource()
                          .saveMidtransServerKey(serverKeyController!.text);

                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(
                          content: Text('Server Key saved'),
                          backgroundColor: AppColors.primary,
                        ),
                      );
                    },
                    label: 'Simpan',
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
