function getProfile() {
  liff
    .getProfile()
    .then(profile => {
      console.log(JSON.stringify(profile));
      window.alert(JSON.stringify(profile));
    })
    .catch(e => {
      console.log(e);
      window.alert(e);
    });
}
function getAccessToken() {
  window.alert(liff.getAccessToken());
}
function getContext() {
  window.alert(JSON.stringify(liff.getContext()));
}

function sendMessage() {
  liff
    .sendMessages([{ type: "text", text: "中文測試 Hello from LIFF2.0" }])
    .then(() => {
      window.alert("Message has been sent");
    })
    .catch(e => {
      window.alert(e);
    });
}

function login() {
  liff.login();
}

function scanCode() {
  liff
    .scanCode()
    .then(result => {
      window.alert(JSON.stringify(result));
    })
    .catch(e => {
      window.alert(e);
    });
}

function openWindow() {
  liff.openWindow({
    url: "https://line.me/1657386781-XARmDogn",
    external: true
  });
}

function closeWindow() {
  liff.closeWindow();
}

function logout() {
  if (liff.isLoggedIn()) {
    liff.logout();
    window.alert("Successfully to Logout");
    location.reload();
  }
}


// function createFlexMessageData() {
//   var flex = {
//     "type": "flex",
//     "altText": redistitle,
//     "contents": redisdata,
//   };

//   return flex;
// }

var _title = "MISA 向您分享了最新消息！";

function createFlexMessageData() {
  var myFlexContent = {
    "type": "bubble",
    "hero": {
      "type": "box",
      "layout": "vertical",
      "contents": [
        {
          "type": "box",
          "layout": "vertical",
          "contents": [
            {
              "type": "text",
              "text": "MISA 新年度 新思維",
              "color": "#ffffff",
              "align": "center",
              "offsetTop": "3px",
              "size": "lg",
              "weight": "bold"
            }
          ],
          "backgroundColor": "#dd462b",
          "paddingAll": "15px"
        },
        {
          "type": "box",
          "layout": "vertical",
          "contents": [
            {
              "type": "image",
              "size": "full",
              "margin": "none",
              "url": "https://upload.cc/i1/2023/01/07/U4iHYt.jpg"
            }
          ],
          "paddingStart": "5px",
          "paddingEnd": "5px",
          "height": "220px",
          "paddingTop": "10px"
        }
      ]
    },
    "body": {
      "type": "box",
      "layout": "vertical",
      "contents": [
        {
          "type": "text",
          "text": "【2023 新的一年如何翻轉慣性思考邏輯",
          "weight": "bold",
          "size": "sm",
          "align": "center",
          "wrap": true
        },
        {
          "type": "text",
          "text": "創造新思維模式】",
          "weight": "bold",
          "size": "sm",
          "align": "center",
          "wrap": true
        },
        {
          "type": "text",
          "text": "「經營管理」的面向非常廣泛",
          "weight": "bold",
          "size": "sm",
          "align": "center",
          "wrap": true,
          "margin": "xxl"
        },
        {
          "type": "text",
          "text": "曾國棟院士透過線上課程視頻",
          "weight": "bold",
          "size": "sm",
          "align": "center",
          "wrap": true,
          "margin": "sm"
        },
        {
          "type": "text",
          "text": "為您分析歸納在職場管理會遇到的問題",
          "weight": "bold",
          "size": "sm",
          "align": "center",
          "wrap": true,
          "margin": "sm"
        },
        {
          "type": "text",
          "text": "跳出框架，充分建立基本的心理建設",
          "weight": "bold",
          "size": "sm",
          "align": "center",
          "margin": "sm",
          "wrap": true
        },
        {
          "type": "text",
          "text": "幫助您減少摸索的時間",
          "weight": "bold",
          "size": "sm",
          "align": "center",
          "margin": "sm",
          "wrap": true
        },
        {
          "type": "text",
          "text": "新年度跨越思維",
          "weight": "bold",
          "size": "sm",
          "align": "center",
          "margin": "xxl"
        },
        {
          "type": "text",
          "text": "讓您瞭解成功領導者在想什麼！",
          "weight": "bold",
          "size": "sm",
          "align": "center",
          "margin": "sm"
        }
      ]
    },
    "footer": {
      "type": "box",
      "layout": "vertical",
      "spacing": "sm",
      "contents": [
        {
          "type": "box",
          "layout": "vertical",
          "contents": [
            {
              "type": "button",
              "style": "link",
              "height": "sm",
              "action": {
                "type": "uri",
                "label": "活動主頁",
                "uri": "https://www.misaglobal.org/event"
              },
              "color": "#ffffff"
            }
          ],
          "backgroundColor": "#dd462b",
          "cornerRadius": "md"
        },
        {
          "type": "box",
          "layout": "vertical",
          "contents": [
            {
              "type": "button",
              "style": "link",
              "height": "sm",
              "action": {
                "type": "uri",
                "label": "活動分享",
                "uri": "https://liff.googo.org/misa-liff/"
              },
              "color": "#dd462b"
            }
          ],
          "cornerRadius": "md",
          "margin": "lg",
          "borderColor": "#dd462b",
          "borderWidth": "normal"
        },
        {
          "type": "box",
          "layout": "vertical",
          "contents": [
            {
              "type": "button",
              "style": "link",
              "height": "sm",
              "action": {
                "type": "uri",
                "label": "官方網站",
                "uri": "https://www.misaglobal.org/"
              },
              "color": "#dd462b"
            }
          ],
          "cornerRadius": "md",
          "margin": "lg",
          "borderColor": "#dd462b",
          "borderWidth": "normal"
        },
        {
          "type": "box",
          "layout": "vertical",
          "contents": [
            {
              "type": "image",
              "url": "https://static.kolable.com/images/misa/logo.png/120?v=1641520480847"
            }
          ],
          "margin": "none",
          "position": "absolute",
          "offsetStart": "108px",
          "offsetTop": "130px"
        }
      ],
      "flex": 0,
      "height": "200px"
    }
  };

  var flex = {
    "type": "flex",
    "altText": _title,
    "contents": myFlexContent,
  };

  return flex;
}

var redisdata = '';

function shareTargetPicker() {

  liff.shareTargetPicker([
    createFlexMessageData()
  ])
    .then(function (res) {
      if (res) {
        // succeeded in sending a message through TargetPicker
        console.log(`[${res.status}] Message sent!`)
      } else {
        const [majorVer, minorVer] = (liff.getLineVersion() || "").split('.');
        if (parseInt(majorVer) == 10 && parseInt(minorVer) < 11) {
          // LINE 10.3.0 - 10.10.0
          // Old LINE will access here regardless of user's action
          console.log('TargetPicker was opened at least. Whether succeeded to send message is unclear')
        } else {
          // LINE 10.11.0 -
          // sending message canceled
          console.log('TargetPicker was closed!')
        }
      }
    }).catch(function (error) {
      // something went wrong before sending a message
      console.log('something wrong happen', error)
    })

  // const url = 'https://liff.googo.org:9898/misa-liff'
  // axios
  //   .get(url)
  //   .then(({data}) => {
  //     redisdata=data.data;
  //     redistitle=data.title;
  //     console.log(redistitle);
  //     console.log(redisdata);

  //     liff.shareTargetPicker([
  //       {
  //         type: "text",
  //         text: "Test!"
  //       }
  //     ])
  //       .then(
  //         console.log("ShareTargetPicker was launched")
  //       ).catch(function (res) {
  //         alert(res);
  //         console.log("Failed to launch ShareTargetPicker", res)
  //       })

  //     // liff
  //     // .shareTargetPicker([
  //     //   createFlexMessageData()      
  //     // ])
  //     // .then(() => {
  //     //   alert("Shared to the friend(s) you picked");
  //     // })
  //     // .catch(function(res) {
  //     //   alert(res);
  //     // });

  //   });
}
