import React, { Component } from "react";
import Constants from "../../Utilities/Constants";

class ResellerConfigBootstrapWidget extends Component {
  constructor(props) {
    super(props);
    this.state = {
      config: {
        favIcon: "",
        headerLogo: "",
        loginLogo: "",
        customCSS: "",
        domain: "",
        resellerName: "",
      },
    };
  }

  componentDidMount() {}

  loadResellerConfig() {
    var self = this;
    let theResellerName = "";
    let theFavIcon = "";

    if (typeof this.props.config == "undefined") {
      //ResselerName
      let tempResellerName = localStorage.getItem(
        Constants.LocalStorageType().ResellerName
      );
      theResellerName =
        typeof tempResellerName != "undefined" && tempResellerName != null
          ? tempResellerName
          : "Chameleon";

      //FavIcon
      theFavIcon = localStorage.getItem(Constants.LocalStorageType().FavIcon);
    } else {
      theResellerName =
        typeof self.props.config.resellerName != "undefined" &&
        self.props.config.resellerName != null
          ? self.props.config.resellerName
          : "Chameleon";
      theFavIcon = self.props.config.favIcon;
    }

    document.title = theResellerName;
    if (theFavIcon && theFavIcon.length > 0) {
      var link =
        document.querySelector("link[rel*='icon']") ||
        document.createElement("link");
      link.type = "image/x-icon";
      link.rel = "shortcut icon";
      link.href = "data:image/png;base64," + theFavIcon;
      //document.getElementsByTagName('head')[0].appendChild(link);
    }
  }

  getCustomCss() {
    this.loadResellerConfig();
    let theCss = "";
    if (typeof this.props.config == "undefined") {
      theCss = localStorage.getItem(Constants.LocalStorageType().CustomCSS);
    } else {
      theCss = this.props.config.customCSS;
    }
    return '<style className="injected-css">' + theCss + "</style>";
  }

  render() {
    return (
      <React.Fragment>
        <div dangerouslySetInnerHTML={{ __html: this.getCustomCss() }}></div>
      </React.Fragment>
    );
  }
}

export default ResellerConfigBootstrapWidget;
