import classnames from "classnames";
import Taro from "@tarojs/taro";
import { Component } from "react";

import {
  Block,
  View,
  Image,
  Text,
  Button,
  PageMeta,
  Video,
} from "@tarojs/components";

import { Popup } from "@antmjs/vantui";

import "../../popup/popup.less";
import "./device-connection-popup.less";

import { go } from "@/utils/traoAPI";

/*** props
 * isLarge            是否大尺寸
 * isShow             是否显示
 * data               数据
 * @confirm           关闭回调
 * ***/
export default class DeviceConnectPopup extends Component<any, any> {
  constructor(props) {
    super(props);
    this.state = {
      name: "蓝牙提示弹窗",
      toRight: false,
      isClick: false,
    };
  }

  async onLoad() {}
  componentDidMount() {}

  componentWillUnmount() {}

  componentDidShow() {}

  componentDidHide() {}

  initData = () => {
    if (this.props.siteData) {
      let arr = JSON.parse(JSON.stringify(this.props.siteData));
      this.setState({ siteList: arr });
    }
  };

  onClose = () => {
    this.props.close();
  };

  onConfirm = () => {
    this.props.confirm();
  };

  onReconnect = () => {
    this.props.reconnect();
  };

  onClickStop = (e) => {
    e.stopPropagation();
  };

  isImage = (url) => {
    if (url) {
      if (url.includes(".mp4")) {
        return false;
      }
    }
    return true;
  };

  render() {
    let {
      isShow,
      data,
      // connectionStatus,
      isConnection,
      connectionSuccess,
      error,
    } = this.props;

    return (
      <Block>
        <PageMeta pageStyle={isShow ? "overflow: hidden;" : ""} />
        <Popup
          style="background-color: #fff;"
          show={isShow}
          closeOnClickOverlay={false}
          round
          overlayStyle="width: 100vw;padding: 0;"
          onClick={this.onClickStop}
        >
          <View
            className="at-icon at-icon-close site-close"
            onClick={this.onClose}
          ></View>

          {!error && (
            <View className={classnames("common-box device-connection-box")}>
              <View
                className={classnames("common-popup-title", "margin-samll")}
              >
                {data.bluetoothConnectingTitle}
              </View>
              <View className="device-popup-content-box">
                {this.isImage(data.bluetoothConnecting) ? (
                  <Video className="videos" src={data.bluetoothConnecting} />
                ) : (
                  <Image
                    className="images"
                    src={data.bluetoothConnecting}
                    mode="aspectFill"
                  />
                )}

                <View className="block">
                  {isConnection ? (
                    <Block>
                      <View className="icon">
                        <Image
                          className="images"
                          src={require("../../../img/selected_success.png")}
                          mode="aspectFill"
                        />
                      </View>
                      <View className="tip">
                        {data.bluetoothConnectingContent}
                      </View>
                    </Block>
                  ) : (
                    <Block>
                      <View className="icon">
                        <Image
                          className="images"
                          src={require("../../../img/select_success.png")}
                          mode="aspectFill"
                        />
                      </View>
                      <View className="tip success">
                        {data.bluetoothConnectingContent}
                      </View>
                    </Block>
                  )}
                </View>
                <View className="block2">
                  {connectionSuccess ? (
                    <Block>
                      <View className="icon">
                        <Image
                          className="images"
                          src={require("../../../img/selected_success.png")}
                          mode="aspectFill"
                        />
                      </View>
                      <View className="tip">正在为您启动今日护理场景</View>
                    </Block>
                  ) : (
                    <Block>
                      <View className="icon">
                        <Image
                          className="images"
                          src={require("../../../img/select_success.png")}
                          mode="aspectFill"
                        />
                      </View>
                      <View className="tip success">
                        正在为您启动今日护理场景
                      </View>
                    </Block>
                  )}
                </View>
              </View>
              <View className="common-popup-btns">
                <Button className="common-popup-btn2" onClick={this.onClose}>
                  取消
                </Button>
                <Button className="common-popup-btn2" onClick={this.onConfirm}>
                  确定
                </Button>
              </View>
            </View>
          )}

          {error && (
            <View className={classnames("common-box", "device-connection-box")}>
              <View
                className={classnames("common-popup-title", "margin-samll")}
              >
                {data.bluetoothConnectFailTitle}
              </View>
              <View className="device-popup-content-box">
                <Video className="videos" src={data.bluetoothConnectFail} />

                <View className="error-block">
                  <View className="tip1">未搜索到设备</View>
                  <View className="tip2">
                    {data.bluetoothConnectFailContent}
                  </View>
                </View>
              </View>
              <View className="common-popup-btns device-popup-btns">
                <Button className="common-popup-btn2" onClick={this.onClose}>
                  取消
                </Button>
                <Button
                  className="common-popup-btn2"
                  onClick={this.onReconnect}
                >
                  重新连接
                </Button>
              </View>
            </View>
          )}
        </Popup>
      </Block>
    );
  }
}
