import BasicPage from "../../../components/basic-page";
import IDCard from "../../../components/identity-card";
import PCard from "../../../components/partner-card";
import IDCardText from "../../../components/identity-card-textbox";

// An object listing pages folks can visit
export async function getStaticProps() {
  const fs = require("fs");
  const yaml = require("js-yaml");
  let PARTNERS = {};
  let emoji_paths = {};

  try {
    let fileContent = fs.readFileSync("./data/identities/partners.yml", "utf8");
    PARTNERS = yaml.load(fileContent);
    const emoji_pattern = new RegExp(/(:[A-Za-z_-]+:)/, "g");
    let emoji = [];
    Object.keys(PARTNERS).forEach((p, i) => {
      Array.from(emoji_pattern[Symbol.matchAll](PARTNERS[p].bio)).forEach(
        (e, i) => {
          emoji.push(e[0]);
        }
      );
      if (Boolean(PARTNERS[p].fields)) {
        Object.keys(PARTNERS[p].fields).forEach((f, i) => {
          Array.from(emoji_pattern[Symbol.matchAll](f)).forEach((e, i) => {
            emoji.push(e[0]);
          });
          Array.from(
            emoji_pattern[Symbol.matchAll](PARTNERS[p].fields[f])
          ).forEach((e, i) => {
            emoji.push(e[0]);
          });
        });
      }
    });
    emoji.forEach((e, i) => {
      if (fs.existsSync(`./public/emoji/${e}.svg`)) {
        emoji_paths[e] = `/emoji/${e}.svg`;
      } else if (fs.existsSync(`./public/emoji/${e}.png`)) {
        emoji_paths[e] = `/emoji/${e}.png`;
      } else {
        emoji_paths[e] = `/emoji/:aliceweh:.png`;
      }
    });
  } catch (e) {
    console.log(e);
  }
  return {
    props: {
      PARTNERS,
      emoji_paths,
    },
  };
}

export default function Partners({ PARTNERS, emoji_paths }) {
  if (PARTNERS) {
    return (
      <BasicPage
        page_title="Partners"
        card_title="Partners!"
        page_button_title="Take me back home!"
      >
        <IDCard
          title="My loves!"
          src="/emoji/:black_heart:.svg"
          alt="Blue heart emoji"
        >
          {Object.keys(PARTNERS).map((partner) => (
            <PCard
              key={PARTNERS[partner].url}
              src={PARTNERS[partner].avi}
              alt={`${partner}'s Avatar`}
              url={PARTNERS[partner].url}
              user={partner}
              fields={PARTNERS[partner].fields}
              bio={PARTNERS[partner].bio}
              emoji_paths={emoji_paths}
            ></PCard>
          ))}
        </IDCard>
      </BasicPage>
    );
  } else {
    return (
      <BasicPage page_title="Partners" card_title="Partners!">
        <IDCard
          title="My loves!"
          src="/emoji/:black_heart:.svg"
          alt="Blue heart emoji"
        >
          <IDCardText
            info={`Oh! It looks like I don't have any partners at the moment.
          
          Oh well!
          `}
          />
        </IDCard>
      </BasicPage>
    );
  }
}
