var SkytalkAuth = function() { return this; };
SkytalkAuth.prototype.endpoint= '/api/auth/status';

/* ログイン済の場合の HTML
   以下の文字列を使うと変換されます。（テスト環境では uri が異なるためです。）
   __username__   :ユーザの名前に変換される
   __logout_uri__ :ログアウトへの URI に変換される
*/
SkytalkAuth.prototype.welcomeHTML= '<div id="head"><img height="5" width="5" alt="" src="/static/images/clear.gif"/><!-- end: #head --></div>'
  + '<div id="bg">'
  + '<h3><img height="19" width="69" alt="ようこそ!" src="/static/images/welcome.png"/></h3>'
  + '<p class="userName">__username__ さん</p>'
  + '<a class="btn" href="__logout_uri__">ログアウト</a>'
  + '<div class="btnMypage"><a href="__my_page__">マイページ</a></div>'
  + '<!-- end: #bg --></div>'
  + '<div id="bottom"><img height="5" width="5" alt="" src="/static/images/clear.gif"/><!-- end: #bottom --></div>';

/* ログイン前の場合の HTML
   以下の文字列を使うと変換されます。（テスト環境では uri が異なるためです。）
   __login_uri__ :ログインフォームへの URI に変換される
   __reminder_uri__ :リマインダ URI に変換される
   __regist_uri__ :新規登録 URI に変換される
*/
SkytalkAuth.prototype.loginFormHTML= '<div id="head"><img alt="" src="/images/clear.gif" height="5" width="5"><!-- end: #head --></div>'
  + '<div id="bg">'
  + '<h3>スカイトークメンバー</h3>'
  + '<div id="loginForm">'
  + '<form action="__login_uri__" method="post">'
  + '<p>メールアドレス</p>'
  + '<input value="" class="txt" name="mail" type="text">'
  + '<p>パスワード</p>'
  + '<input value="" class="txt" name="password" style="margin-bottom: 5px;" type="password"><input name="submit" value="ログイン" class="login" type="submit">'
  + '</form>'
  + '<p class="passWord"><a href="__reminder_uri__">パスワードを忘れた方</a></p><!-- end #loginForm --></div>'
  + '<!-- end #bg --></div>'
  + '<div id="bottom"><img alt="" src="/images/clear.gif" height="5" width="5"><!-- end: #head --></div>'


SkytalkAuth.prototype.showStatus = function () {
   var __this = this;
   $.getJSON(
      this.endpoint,
      {},
      function (data) {
         if ( data.response ) {
            if ( data.status ) {
               __this.showWelcome(data);
            }
            else {
               __this.showLoginForm(data);
            }
         }
         else {
            // データ取得失敗時はログインフォームを出します
            __this.showLoginForm(data);
         }
      }
   );
};

SkytalkAuth.prototype.showWelcome = function (data) {
   var html = this.welcomeHTML;
   html = html.replace(/__username__/g, data.user.nickname);
   html = html.replace(/__logout_uri__/g, data.uri.logout);
   html = html.replace(/__my_page__/g, data.uri.my_page);
   $('#LoginStatus').html(html);
}

SkytalkAuth.prototype.showLoginForm = function (data) {
   var html = this.loginFormHTML;
   html = html.replace(/__login_uri__/g, data.uri.login);
   html = html.replace(/__reminder_uri__/g, data.uri.reminder);
   html = html.replace(/__regist_uri__/g, data.uri.regist);
   $('#LoginStatus').html(html);
}

$(document).ready(function() {
    var auth = new SkytalkAuth();
    auth.showStatus();
});

