import 'package:flutter/material.dart';

class ProtocolSwitchWidget extends StatelessWidget {
  final bool useSSL;

  final ValueChanged<bool> onChanged;

  const ProtocolSwitchWidget({
    super.key,
    required this.useSSL,
    required this.onChanged,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(2),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(4),
        border: Border.all(color: Colors.grey),
      ),
      child: Row(
        children: [
          _OptionWidget(
            text: "HTTP",
            checked: !useSSL,
            onTap: () => onChanged.call(false),
          ),
          _OptionWidget(
            text: "HTTPS",
            checked: useSSL,
            onTap: () => onChanged.call(true),
          ),
        ],
      ),
    );
  }
}

class _OptionWidget extends StatelessWidget {
  final String text;
  final bool checked;
  final VoidCallback onTap;

  const _OptionWidget({
    super.key,
    required this.text,
    required this.checked,
    required this.onTap,
  });
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        height: 32,
        width: 80,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4),
          color: checked ? Theme.of(context).primaryColor : null,
        ),
        child: Text(
          text,
          style: TextStyle(color: checked ? Colors.white : null, fontSize: 16),
        ),
      ),
    );
  }
}
