function Service() {

  let m_LineUserId;
  let m_LineName;
  let m_LinePicture;
  let m_UserModel;
  let m_data = {
    
  };


  function init() {
    console.log('GameService init')
    return liffInit().then((profile) => {
      
      // // 測試用資料
      // const profile = {
      //   userId: "Ua89aacd560a328ece0fec5584435046f",
      //   displayName: "Tim",
      //   pictureUrl: "https://sprofile.line-scdn.net/0hAvrrYLRRHllfCwoU_RtgJi9bHTN8ekdLdWRZPmJYQGxnPFwLdG5Za29YSTkyb1AGID9XbzoLEmlTGGk_QV3ibVg7QG5mOVANdm5Uug",
      // }

      m_LineUserId = profile.userId || '';

      if (['Ud3c9b3df7c92c51c94bc766fc7189bf5', 'U1856545fb999ec7ca759f34685e81207', 'U7ce424b056a71ecd30d5d5f34d37873d', 'U7cc9097f3663b4176054f5e33e31e194'].includes(m_LineUserId)) {
        var vConsole = new VConsole();
      }

      
      m_LineName = profile.displayName || '';
      m_LinePicture = profile.pictureUrl || '';
      m_UserModel = new UserModel( m_LineUserId);

      console.log('lineUserId:', m_LineUserId);

      

      // 測試用
      // return Promise.resolve(profile);
      return profile;
    }).catch((error) => {
      alert('Liff Init Error \n Message:' + error.message);
      console.log('[GameService] Liff Init Error \n Name:' + error.name + '\n Message:' + error.message);
    });
  }

  /**
   * 用戶登入
   * @returns {TypeUserResponse} Promise
   */
  function userLogin() {
    console.log("userLogin")
    return m_UserModel.loginUser().then((response) => {
      m_data = response.data;
      getUserData();
      console.log(response)
      return response;
    });
  }

  // 測試用
  // function userLogin() {
  //   console.log("userLogin")
  //   return new Promise(resolve => {
  //     getUserData();
  //     resolve({
  //       m_data
  //     });
  //   })
  // }

  /**
   * 用戶註冊
   * @param {object} _formdata 
   * @returns {TypeUserResponse} Promise
   */
  function userRegister(_formdata) {
    return m_UserModel.registerUser(_formdata);
  }

 // 測試用
  // function userRegister(_formdata) {
  //   // console.log(JSON.stringify(_formdata))
  //   return new Promise(resolve => {
  //     resolve("userRegister success");
  //   })
  // }

    /**
   * 用戶更新照片
   * @returns {TypeUserResponse} Promise
   */
  function userUpdatePhoto(base64Image) {
    return m_UserModel.updatePhoto(base64Image);
  }

  // 測試 
  // function userUpdatePhoto(base64Image) {
  //   return new Promise(resolve => {
  //     resolve ('success');
  //   })
  // }

  function getUserData() {
    return m_data;
  }
  
  /**
   * 驗證表單
   * @param {object} _formdata 
   * @returns { boolean | error }
   */
  function verifyUserForm(_formdata) {
    return m_UserModel.verifyUserForm(_formdata);
  }

  function sendMessage(_theMessage) {
    return liff.sendMessages([
      {
        type: 'text',
        text: _theMessage
      }
    ]).then(() => {
      console.log('message sent');
      return 'message sent';
    }).catch((err) => {
      throw new Error(err);
    });
  }
  
  function sendCompleteMessage() {
    return sendMessage('健康護照填寫完成！').then(() => {
      return 'message sent';
    });
  }

  {
    // init();
  }

  return {
    init,
    userLogin,
    userRegister,
    verifyUserForm,
    getUserData,
    userUpdatePhoto,
    sendCompleteMessage
  }
}

let _inatance
const GameService = {
  getInstanse: () => {
    if (!_inatance) {
      _inatance = new Service();
    }
    return _inatance;
  }
}