import 'package:example/test_data.dart';
import 'package:fis_lib_media_rt/widgets/index.dart';
import 'package:flutter/material.dart';

class TestMainPage extends StatefulWidget {
  const TestMainPage({super.key});

  @override
  State<TestMainPage> createState() => _TestMainPageState();
}

class _TestMainPageState extends State<TestMainPage> {
  final data = TestDataStruct();

  final uidInputController = TextEditingController();

  @override
  void initState() {
    data.update = () {
      setState(() {});
    };
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      if (mounted) {
        data.init();
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          buildControlBlock(context),
          buildPreviewBlock(context),
        ],
      ),
    );
  }

  Widget buildControlBlock(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 8),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text("RoomId: ${data.roomId}"),
          const SizedBox(height: 8),
          buildUserIdInput(),
          const SizedBox(height: 8),
          if (data.entered) ...[
            ElevatedButton(
              onPressed: () {
                data.exit();
              },
              child: const Text("Exit Room"),
            ),
            const SizedBox(height: 8),
            if (!data.localStarted)
              ElevatedButton(
                onPressed: () {
                  data.startLocal();
                },
                child: const Text("Start Local Preview"),
              ),
            if (data.localStarted) buildLocalStarted(),
            ...combinCaputreWidgets(),
          ],
          if (!data.entered)
            ElevatedButton(
              onPressed: () {
                data.userId = uidInputController.text;
                data.enter();
              },
              child: const Text("Enter Room"),
            ),
        ],
      ),
    );
  }

  List<Widget> combinCaputreWidgets() {
    return [
      const SizedBox(height: 8),
      ElevatedButton(
        onPressed: () {
          data.captureVid();
        },
        child: const Text("Capture Once"),
      ),
      const SizedBox(height: 8),
      ElevatedButton(
        onPressed: () {
          data.startCaptureVid();
        },
        child: const Text("Start Record"),
      ),
      const SizedBox(height: 8),
      ElevatedButton(
        onPressed: () {
          data.stopCaptureVid();
        },
        child: const Text("Stop Record"),
      ),
    ];
  }

  Widget buildLocalStarted() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        ElevatedButton(
          onPressed: () {
            data.stopLocal();
          },
          child: const Text("Stop Local Preview"),
        ),
        ElevatedButton(
          onPressed: () {
            data.setVideoMute(!data.isVideoMuted);
          },
          child: Text("${data.isVideoMuted ? 'unmute' : 'mute'} Video"),
        ),
      ],
    );
  }

  Widget buildUserIdInput() {
    if (data.entered) {
      return Text("UserId: ${data.userId}");
    } else {
      return SizedBox(
        height: 36,
        child: Row(
          children: [
            const Text("UserId: "),
            Expanded(child: TextField(controller: uidInputController)),
          ],
        ),
      );
    }
  }

  Widget buildPreviewBlock(BuildContext context) {
    final size = MediaQuery.of(context).size;
    const double whr = 640 / 480; // TODO:
    return Column(
      children: [
        const SizedBox(height: 6),
        buildLocalPreview(Size(size.width, size.width / whr)),
        const SizedBox(height: 6),
        buildRemotePreview(Size(size.width, size.width / whr)),
        const SizedBox(height: 6),
      ],
    );
  }

  Widget buildLocalPreview(Size size) {
    final Widget viewWidget;
    if (data.localChannel != null) {
      viewWidget = FRTPublishPreviewView(data.localChannel!);
    } else {
      viewWidget = const Text(
        "No Signal",
        style: TextStyle(color: Colors.white),
      );
    }
    return Container(
      width: size.width,
      height: size.height,
      color: Colors.black54,
      child: Stack(
        children: [
          SizedBox(
            width: size.width,
            height: size.height,
            child: Center(
              child: viewWidget,
            ),
          ),
          const Positioned(
            top: 2,
            left: 2,
            child: Text(
              "Local",
              style: TextStyle(color: Colors.white),
            ),
          ),
        ],
      ),
    );
  }

  Widget buildRemotePreview(Size size) {
    final Widget viewWidget;
    if (data.remoteChannel != null) {
      viewWidget = FRTPlayChannelView(data.remoteChannel!);
    } else {
      viewWidget = const Text(
        "No Signal",
        style: TextStyle(color: Colors.white),
      );
    }
    return Container(
      width: size.width,
      height: size.height,
      color: Colors.black54,
      child: Stack(
        children: [
          SizedBox(
            width: size.width,
            height: size.height,
            child: Center(
              child: viewWidget,
            ),
          ),
          const Positioned(
            top: 2,
            left: 2,
            child: Text(
              "Remote",
              style: TextStyle(color: Colors.white),
            ),
          ),
        ],
      ),
    );
  }
}
