<!--
 * @Author: ChenChanghua
 * @Date: 2023-03-08 14:52:08
 * @Description: file content
-->
<script setup>
import { MetroWidget, HORIZONTAL, useFocusHub } from "jsview";
import Item from "./Item.vue";
import { onMounted } from "vue";

const TAG = "MetroWidgetDemo";

const focusHub = useFocusHub();

const randomColor = () => {
  let randomColor = Math.round(Math.random() * 2 ** 24).toString(16);
  return (
    "#" + new Array(6 - randomColor.length).fill("0").join("") + randomColor
  );
};

const provideData = () => {
  const data = [
    {
      width: 90,
      height: 90,
      marginRight: 10,
      marginBottom: 10,
      color: randomColor(),
      content: 0,
    },
    {
      width: 90,
      height: 90,
      marginRight: 10,
      marginBottom: 10,
      color: randomColor(),
      content: 1,
    },
    {
      width: 90,
      height: 90,
      marginRight: 10,
      marginBottom: 10,
      color: randomColor(),
      content: 2,
    },
    {
      width: 190,
      height: 190,
      marginRight: 10,
      marginBottom: 10,
      color: randomColor(),
      content: 3,
    },
    {
      width: 90,
      height: 90,
      marginRight: 10,
      marginBottom: 10,
      color: randomColor(),
      content: 4,
    },
    {
      width: 90,
      height: 90,
      marginRight: 10,
      marginBottom: 10,
      color: randomColor(),
      content: 5,
    },
  ];

  for (let i = 0; i < 5; i++) {
    data.push(
      {
        width: 90,
        height: 190,
        marginRight: 10,
        marginBottom: 10,
        color: randomColor(),
        content: 6 + i * 2,
      },
      {
        width: 90,
        height: 90,
        marginRight: 10,
        marginBottom: 10,
        color: randomColor(),
        content: 6 + i * 2 + 1,
      }
    );
  }
  return data;
};

const measures = (data) => {
  return {
    width: data.width,
    height: data.height,
    marginRight: data.marginRight,
    marginBottom: data.marginBottom,
  };
};

// callbacks
const onEdge = (edgeInfo) => {
  console.log(TAG, "onEdge", edgeInfo);
};

const onFocus = () => {
  console.log(TAG, "onFocus");
};

const onBlur = () => {
  console.log(TAG, "onBlur");
};

const onScroll = (start, range, totalSize) => {
  console.log(TAG, "onScroll", start, range, totalSize);
};

const widgetLayout = {
  left: 50,
  top: 50,
  width: 500,
  height: 300,
};

onMounted(() => {
  //组件mount后设置焦点
  focusHub.setFocus("mainMw");
});
</script>

<template>
  <div
    :style="{
      width: 1920,
      height: 1080,
      backgroundColor: '#007788',
    }"
  />
  <metro-widget
    name="mainMw"
    :left="widgetLayout.left"
    :top="widgetLayout.top"
    :width="widgetLayout.width"
    :height="widgetLayout.height"
    :provideData="provideData"
    :direction="HORIZONTAL"
    :measures="measures"
    :onEdge="onEdge"
    :onFocus="onFocus"
    :onBlur="onBlur"
    :onScroll="onScroll"
  >
    <template #renderItem="{ data, onAction }">
      <item :data="data" :onAction="onAction" />
    </template>
  </metro-widget>
</template>