function genCard(vcard) {
  // 名片預覽
  let vcardLink;
  if (vcard.url.trim().length > 0) {
    if (vcard.nc_func.indexOf("nourl") !== -1) {
      vcardLink = `{
        "type": "text",
        "text": "${vcard.url}",
        "color": "#ffffff",
        "size": "sm"
      },
      `;
    } else {
      vcardLink = `
      {
        "type": "text",
        "text": "${vcard.url}",
        "color": "#ffffff",
        "size": "sm",
        "action": {
            "type": "uri",
            "label": "action",
            "uri": "${vcard.url}"
        }
      },
      `;
    }
  } else {
    vcardLink = "";
  }

  let vcardAvatar;
  if (vcard.avatar.trim().length > 0) {
    vcardAvatar = `
    {
      "type": "image",
      "url": "${vcard.avatar}",
      "size": "full",
      "aspectRatio": "1:1",
      "aspectMode": "cover",
      "action": {
        "type": "uri",
        "label": "action",
        "uri": "${vcard.nfcurl}&cardid=1"
      }    
    }
    `;
  } else {
    vcardAvatar = "";
  }

  let vcardAddr;
  if (vcard.address.trim().length > 0) {
    vcardAddr = `
      {
        "type": "text",
        "text": "${vcard.address}",
        "color": "#ffffff",
        "size": "sm",
        "margin": "sm",
        "wrap": true,
        "action": {
            "type": "uri",
            "label": "action",
            "uri": "https://www.google.com.tw/maps/place/${encodeURIComponent(
              vcard.address
            )}"
        }
      },      
      `;
  } else {
    vcardAddr = "";
  }

  let vcardTel;
  if (vcard.tel.trim().length > 0) {
    vcardTel = `
    {
      "type": "text",
      "text": "${vcard.tel}",
      "color": "#ffffff",
      "size": "lg",
      "wrap": true,
      "action": {
          "type": "uri",
          "label": "action",
          "uri": "https://www.google.com.tw/maps/place/${encodeURIComponent(
            vcard.address
          )}"
      }
    },
    `;
  } else {
    vcardTel = "";
  }

  let vcardPhone;
  if (vcard.phone.trim().length > 0) {
    vcardPhone = `
    {
      "type": "text",
      "text": "${vcard.phone}",
      "size": "lg",
      "margin": "sm",
      "action": {
        "type": "uri",
        "label": "action",
        "uri": "tel:${vcard.phone}"
      }
    }
    `;
  } else {
    vcardPhone = "";
  }

  let vcardEmail;
  if (vcard.email.trim().length > 0) {
    vcardEmail = `
    {
      "type": "text",
      "text": "${vcard.email}",
      "color": "#ffffff",
      "size": "sm",
      "margin": "sm",
      "action": {
        "type": "uri",
        "label": "action",
        "uri": "mailto:${vcard.email}"
      }
    },
    `;
  } else {
    vcardEmail = "";
  }

  // 名片發送

  let card = `
    {
      "altText": "UTel電子名片",
      "type": "flex",
      "contents": 
      {
        "type": "bubble",
        "size": "giga",
        "body": {
          "type": "box",
          "layout": "horizontal",
          "height": "290px",
          "paddingAll": "0px",
          "action": {
            "type": "uri",
            "label": "action",
            "uri": "${vcard.nfcurl}"
          },
          "contents": [
            {
              "position": "relative",
              "type": "box",
              "layout": "vertical",
              "width": "35%",
              "height": "100%",
              "paddingAll": "15px",
              "alignItems": "center",
              "contents": [
                {
                  "type": "box",
                  "layout": "vertical",
                  "width": "80%",
                  "margin": "none",
                  "borderWidth": "bold",
                  "borderColor": "#f5c520",
                  "cornerRadius": "150px",
                  "contents": [${vcardAvatar}]
                },
                {
                  "type": "text",
                  "text": "${vcard.name}",
                  "size": "xl",
                  "weight": "bold",
                  "margin": "md"
                },
                {
                  "type": "text",
                  "text": "${vcard.title}",
                  "size": "md",
                  "weight": "regular",
                  "color": "#666666"
                },                
                ${vcardPhone}
              ]              
            },
            {
              "position": "relative",
              "type": "box",
              "layout": "vertical",
              "width": "66%",
              "height": "100%",
              "justifyContent": "space-between",
              "paddingTop": "4%",
              "paddingBottom": "5%",
              "paddingStart": "4%",
              "paddingEnd": "4%",
              "backgroundColor": "#353e45",
              "contents": [
                {
                  "type": "box",
                  "layout": "vertical",
                  "contents": [
                    ${vcardTel}
                    {
                      "type": "text",
                      "text": "${vcard.company}",
                      "color": "#ffffff",
                      "size": "xl",
                      "weight": "bold"
                    }
                  ]
                },
                {
                  "type": "box",
                  "layout": "vertical",                  
                  "contents": [                    
                    ${vcardLink}
                    ${vcardEmail}
                    ${vcardAddr}                    
                    {
                      "type": "box",
                      "layout": "horizontal",
                      "justifyContent": "space-between",
                      "margin": "md",
                      "contents": [
                        {
                          "type": "box",
                          "layout": "vertical",
                          "width": "48%",
                          "height": "40px",
                          "justifyContent": "center",
                          "alignItems": "center",
                          "borderWidth": "light",
                          "borderColor": "#dddddd",
                          "cornerRadius": "lg",                          
                          "contents": [
                              {
                                  "type": "text",
                                  "text": "加入好友",
                                  "color": "#dddddd"
                              }
                          ],
                          "action": {
                              "type": "uri",
                              "label": "action",
                              "uri": "https://line.naver.jp/ti/p/~${vcard.line}"
                            }                     
                        },
                        {
                          "type": "box",
                          "layout": "vertical",                          
                          "width": "48%",
                          "height": "40px",
                          "justifyContent": "center",
                          "alignItems": "center",
                          "borderWidth": "light",
                          "borderColor": "#dddddd",
                          "cornerRadius": "lg",                          
                          "contents": [
                              {
                                  "type": "text",
                                  "text": "分享名片",
                                  "color": "#dddddd"
                              }
                          ],
                          "action": {
                            "type": "uri",
                            "label": "分享名片",
                            "uri": "${import.meta.env.VITE_APP_SEND_URL}/?userid=${vcard.user_id}&cardid=1&tpl=3"
                          }
                        }
                      ]                      
                    }
                  ]
                }
              ]              
            },
            {
              "position": "absolute",
              "type": "box",
              "layout": "vertical",
              "width": "67%",
              "height": "100%",
              "justifyContent": "flex-start",
              "offsetEnd": "none",
              "contents": [
                {
                  "position": "relative",
                  "type": "image",
                  "url": "https://369cycle.zltest.com.tw/tggo/template/bg-01s.png",
                  "size": "full",
                  "aspectRatio": "3:1",
                  "offsetTop": "15%"
                }
              ]             
            }
          ]
        }
      }
    }
    `;
  return { card };
}

export { genCard };
