require_dependency 'account_controller'

module OpenidAccountControllerPatch

    def self.included(base)
        base.extend(ClassMethods)
        base.send(:include, InstanceMethods)
        base.class_eval do
            unloadable

            alias_method :open_id_authenticate, :fixed_open_id_authenticate
        end
    end

    module ClassMethods
    end

    module InstanceMethods

        OPENID_AX_EMAIL     = 'http://axschema.org/contact/email'
        OPENID_AX_FIRSTNAME = 'http://axschema.org/namePerson/first'
        OPENID_AX_LASTNAME  = 'http://axschema.org/namePerson/last'
        OPENID_AX_LANGUAGE  = 'http://axschema.org/pref/language'

        def fixed_open_id_authenticate(openid_url)
            options = [ :nickname, :fullname, :email ]

            # Google OpenID
            options << OPENID_AX_EMAIL     # mail
            options << OPENID_AX_FIRSTNAME # firstname
            options << OPENID_AX_LASTNAME  # lastname
            options << OPENID_AX_LANGUAGE  # language

            authenticate_with_open_id(openid_url, :required => options, :return_to => signin_url, :method => :post) do |result, identity_url, registration|
                if result.successful?
                    user = User.find_or_initialize_by_identity_url(identity_url)

		    if user.new_record? 
                        redirect_to(home_url) && return unless Setting.self_registration?

                       if Rails::VERSION::MAJOR >= 3
                           ax_registration = OpenID::AX::FetchResponse.from_success_response(request.env[Rack::OpenID::RESPONSE])
                       else
                           ax_registration = registration
                       end

                        if ax_registration[OPENID_AX_EMAIL]
			    userByEmail = User.find_by_mail(ax_registration[OPENID_AX_EMAIL].first)                                    
			    if userByEmail
			      user = userByEmail
			      user.identity_url = identity_url
			      user.save()
			    else
			      user.login = ax_registration[OPENID_AX_EMAIL].first
			      user.mail = ax_registration[OPENID_AX_EMAIL].first
			    end
                        else
                            user.login = ax_registration['nickname'] unless ax_registration['nickname'].nil?
                            user.mail = ax_registration['email'] unless ax_registration['email'].nil?
                        end
                        user.firstname, user.lastname = ax_registration['fullname'].split(' ') unless ax_registration['fullname'].nil?
                        if ax_registration[OPENID_AX_FIRSTNAME]
                            user.firstname = ax_registration[OPENID_AX_FIRSTNAME].first
                        end
                        if ax_registration[OPENID_AX_LASTNAME]
                            user.lastname = ax_registration[OPENID_AX_LASTNAME].first
                        end
                        if ax_registration[OPENID_AX_LANGUAGE]
                            lang = ax_registration[OPENID_AX_LANGUAGE].first
                            if lang
                                language = find_language(lang) || find_language(lang.split('-').first)
                                if language
                                    user.language = language.to_s
                                    set_language_if_valid(language)
                                end
                            end
                        end
                        user.random_password
                        user.register

                        settings = Setting.plugin_openid_fix

                        if settings[:self_registration].nil?
                            self_registration = Setting.self_registration == '2' ? '2' : '3'
                        elsif settings[:self_registration].empty?
                            self_registration = Setting.self_registration
                        else
                            self_registration = settings[:self_registration]
                        end

                        case self_registration
                        when '1'
                            register_by_email_activation(user) do
                                onthefly_creation_failed(user)
                            end
                        when '3'
                            register_automatically(user) do
                                onthefly_creation_failed(user)
                            end
                        else
                            register_manually_by_administrator(user) do
                                onthefly_creation_failed(user)
                            end
                        end
                    else
                        if user.active?
                            successful_authentication(user)
                        else
                            account_pending
                        end
                    end
                end
            end
        end

    end

end
