import 'package:easyweb/easyweb.dart';
import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:easyweb/base/minisdk.dart';
import 'package:easydevice/easydevice.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';
import 'package:vbvs_app/pages/mh_page/homepage/controller/mht_home_controller.dart';
import 'package:vbvs_app/pages/mh_page/room_picker.dart';

class WebviewTestModel {
  WebviewTestModel();

  //EasyFlutter Start[Model]

  //EasyFlutter End
}

class WebviewTestController extends GetControllerEx<WebviewTestModel> {
  var selectDevice = {};
  var bluetooth = 0;
  WebviewTestController() : super(WebviewTestModel()) {
    web = WebviewHelper(
      jsbridge: buildsdk(
        father: this,
        clientId: '494641114',
        // dbgserverUrl: 'ws://192.168.1.2:9001',
      ),
      settings: buildsettings(),
      params: PlatformHeadlessInAppWebViewCreationParams(
        initialUrlRequest: URLRequest(
          url: WebUri(
              'https://wyf.it.real.he-info.cn:94/goods-front/index.html'),
        ),
        onLoadStop: (controller, url) {
          setState(() => ready.value = true);
          MHTHomeController deviceController = Get.find();
          web.jsbridge!.dart
              .updateDeviceList(deviceController.deviceList.values);
        },
      ),
    );
    try {
      web.jsbridge!.bind((bridge) {
        var minible = Minibleapp(bridge: bridge);
        // ef.kvroot.devicelist.listen((x) {});
        bridge.sdk.updateDeviceRoute((args) async {
          ef.log('updateDeviceRoute: $args');
          selectDevice['blueToothStatus'] = bluetooth;
          Get.toNamed("${args[0]}", arguments: selectDevice);
          return true;
        });
        bridge.sdk.selectDevice((args) async {
          ef.log('selectDevice: $args');
          selectDevice = args[0];
          return true;
        });
        bridge.sdk.updateBlueToothStatus((args) async {
          ef.log('updateBlueToothStatus: $args');
          bluetooth = args[0];
          return true;
        });
        //sdk定义接口
        bridge.sdk.querySelectDevice((args) async {
          ef.log('updateBlueToothStatus: $args');
          // bluetooth = args[0];
          MHTHomeController deviceController = Get.find();
          final allDevices = deviceController.deviceList.values
              .expand((list) => list)
              .toList();
          return allDevices;
        });
      });
      web
          //.file(
          //  url: 'https://wsl.it.real.he-info.cn:94/webdemo/web.zip',
          //  pretag: 'https://wsl.it.real.he-info.cn:94/webdemo/',
          //)
          .network()
          .then((value) {
        if (value) {
          setState(() {
            ready.value = true;
          });
        }
      });
    } catch (e, s) {
      ef.log('$e,$s');
    }
  }
  late WebviewHelper web;
  var ready = false.obs;
  var cnt = 0.obs;
  //EasyFlutter End
}

class WebviewTestView extends GetComponent<WebviewTestController> {
  WebviewTestView({super.key, super.oncreate});

  @override
  Widget build(BuildContext context) {
    UserInfoController userInfoController = Get.find();
    MHTHomeController deviceController = Get.find();
    deviceController.getDeviceList().then((x) {
      if (controller.web.jsbridge!.inited) {
        //发送测试消息给webview
        controller.web.jsbridge!.dart
            .updateDeviceList(deviceController.deviceList.values);
      }
    });

    return Scaffold(
      backgroundColor: Colors.black12,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: (userInfoController.model.login! != null &&
                userInfoController.model.login! == 1)
            ? MainAxisAlignment.start
            : MainAxisAlignment.center,
        children: [
          Obx(() {
            if (userInfoController.model.login! == null ||
                userInfoController.model.login! == 0) {
              return Center(
                // 包一层 Center，强制水平和垂直都居中
                child: Text(
                  "登录进行设备控制".tr,
                  style: TextStyle(color: Colors.white, fontSize: 30.rpx),
                ),
              );
            }

            return Expanded(
              child: Align(
                alignment: Alignment.topLeft,
                child: controller.ready.value
                    ? ((controller.cnt.value % 2 == 0)
                        ? controller.web.build(
                            key: ValueKey("A"),
                            headless: false,
                          )
                        : controller.web.build(
                            key: ValueKey("B"),
                            headless: false,
                          ))
                    : Container(),
              ),
            );
          }),
        ],
      ),
    );
  }
}
