// copyright (c) 2012 conn.dwango.jp
/**
 * @author dwango.jp
 * @constructor
 * @version 1.0.0
 */
var DConnect = function(id) {

    this.fragment = (function() {
        var response = {};
        var fragment = location.hash.substr(1);
        if (fragment.length > 0) {
            fragment = fragment.split('&');
            for (var i = 0; i < fragment.length; i++) {
                field = fragment[i].split('=');
                response[field[0]] = field[1];
            }
        }
        return response;
    })();

    this.host = 'conn.dwango.jp';
    this.id = id;
    this.inner = document.createElement('iframe');
    this.scheme = '//';
};

DConnect.prototype = {

    /**
     * @param string uri
     * @param string state
     */
    auth : function(uri, state) {

        var l = this.scheme + this.host;
        l += '/auth';
        l += '?client_id=' + this.id;
        l += '&redirect_uri=' + encodeURIComponent(uri);
        l += '&state=' + encodeURIComponent(state);
        location.href = l;
    },

    itemizedBill : function(callback, fields) {

        if (!this._isAccessToken()) {

            // response code, message.
            return eval(callback + '({code:400,message:"access_token"})');
        }

        var s = this.scheme + this.host;
        s += '/api/billItemized';
        s += '?access_token=' + this.fragment.access_token;
        s += '&callback=' + callback;

        if ( typeof fields != 'undefined') {

            // request options.
            if ( typeof fields.id != 'undefined')
                s += '&id=' + fields.id;
            if ( typeof fields.billId != 'undefined')
                s += '&billId=' + fields.billId;
            if ( typeof fields.displayName != 'undefined')
                s += '&displayName=' + encodeURIComponent(fields.displayName);
            if ( typeof fields.itemsPerPage != 'undefined')
                s += '&itemsPerPage=' + fields.itemsPerPage;
            if ( typeof fields.startIndex != 'undefined')
                s += '&startIndex=' + fields.startIndex;
            if ( typeof fields.sortOrder != 'undefined')
                s += '&sortOrder=' + fields.sortOrder;
        }
        this._appendScript(s);
    },

    /**
     * @param string email
     * @param string password
     */
    login : function(email, password) {

        this.inner.src = 'https://' + this.host + '/login';
        this.inner.src += '?client_id=' + this.id;
        this.inner.src += '&email=' + encodeURIComponent(email);
        this.inner.src += '&password=' + encodeURIComponent(password);
        this.inner.style.display = 'none';
        document.body.appendChild(this.inner);
    },

    logout : function() {

        this.inner.src = this.scheme + this.host + '/logout';
        this.inner.src += '?client_id=' + this.id;
        this.inner.style.display = 'none';
        document.body.appendChild(this.inner);
    },

    /**
     * OpenID Authentication
     */
    login_au: function(redirect_uri) {
        this._openId('au', redirect_uri);
    },

    login_docomo: function(redirect_uri) {
        this._openId('docomo', redirect_uri);
    },

    login_eznumber: function(redirect_uri) {
        this._openId('eznumber', redirect_uri);
    },

    login_softbank: function(redirect_uri) {
        this._openId('softbank', redirect_uri);
    },

    /**
     * @param string callback
     */
    tokeninfo : function(callback) {

        if (!this._isAccessToken()) {

            // response code, message.
            return eval(callback + '({code:400,message:"access_token"})');
        }

        var s = this.scheme + this.host;
        s += '/api/tokeninfo';
        s += '?access_token=' + this.fragment.access_token;
        s += '&callback=' + callback;
        this._appendScript(s);
    },

    /**
     * @param string callback
     */
    userinfo : function(callback) {

        if (!this._isAccessToken()) {

            // response code, message.
            return eval(callback + '({code:400,message:"access_token"})');
        }

        var s = this.scheme + this.host;
        s += '/api/userinfo';
        s += '?access_token=' + this.fragment.access_token;
        s += '&callback=' + callback;
        this._appendScript(s);
    },

    /**
     * @private
     */
    _openId : function(provider, uri) {
        var l = 'https://' + this.host;
        l += '/login_' + provider;
        l += '?client_id=' + this.id;
        l += '&redirect_uri=' + encodeURIComponent(uri);
        location.href = l;
    },

    /**
     * @private
     */
    _appendScript : function(src) {

        var script = document.createElement('script');
        script.src = src;

        document.head.appendChild(script);
        document.head.removeChild(script);
    },

    /**
     * @private
     */
    _isAccessToken : function() {
        return typeof this.fragment.access_token != 'undefined';
    }
};
