import * as React from "react";
import _ from "lodash";

import EditorObject from "@sc/modules/page/Builder/EditorObject";
import { updateSection } from "@sc/modules/v2/Properties/components/generator";
import PropertiesBuilder from "@sc/modules/v2/Properties/components/generator";
import { SectionTypes } from "@sc/modules/v2/Properties/types";

import { FacebookLoginComponent } from "./live";
import { PluginProps } from "./types";
import { V1ObjectWrapper, convertProperties } from "../V1ObjectWrapper";

import buttonSettings from "./settings";

const Properties = (props) => {
  const { updateComponentSettings, updateComponentStyle, settings } = props;
  const { properties } = buttonSettings;
  const { sections } = properties.main;

  const appIdSection = updateSection(sections, "appId", {
    value: _.get(settings, "appId", ""),
    onChange: (appId) => {
      updateComponentSettings(settings.id, { ...settings, appId });
    },
  });

  const captionSection = updateSection(appIdSection, "caption", {
    value: _.get(settings, "buttonSettings.caption1", ""),
    onChange: (caption1) => {
      updateComponentSettings(settings.id, {
        ...settings,
        buttonSettings: { ...settings.buttonSettings, caption1 },
      });
    },
  });

  const colorSection = updateSection(captionSection, SectionTypes.COLORPICKER, {
    color: _.get(settings, "properties.backgroundColor", ""),
    onChange: (color) => {
      updateComponentStyle(settings.id, { backgroundColor: color.hex });
    },
  });

  return (
    <PropertiesBuilder
      data={{
        ...properties,
        main: { ...properties.main, sections: colorSection },
      }}
      {...props}
    />
  );
};

/**
 * Facebook Login Component
 */

export const EditorFacebookLoginButton: React.FC<PluginProps> = (props) => {
  const { id, settings } = props;
  const properties = convertProperties(settings.properties);
  const {
    backgroundColor,
    color,
    fontSize,
    fontFamily,
    padding,
  } = settings.properties;
  const buttonSettings = {
    ...settings.buttonSettings,
    style: {
      ...properties,
      backgroundColor,
    },
    caption1style: {
      color,
      fontSize,
      fontFamily,
      padding,
    },
  };

  return (
    <V1ObjectWrapper settings={settings} showOverlay={true}>
      <EditorObject
        title="Facebook Login"
        id={id}
        {...props}
        PropertiesView={Properties}
        showOverlay
      >
        <FacebookLoginComponent
          {...settings}
          properties={properties}
          buttonSettings={buttonSettings}
        />
      </EditorObject>
    </V1ObjectWrapper>
  );
};

export default EditorFacebookLoginButton;
