/*
 * Copyright (C) 2012 Antew | antewcode@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.antew.redditinpictures.library.reddit;

import java.util.List;

import android.content.ContentValues;

import com.antew.redditinpictures.library.interfaces.ContentValuesOperation;
import com.antew.redditinpictures.sqlite.RedditContract;
import com.google.gson.annotations.SerializedName;

public class RedditLoginResponse implements ContentValuesOperation {
    public static final String TAG = RedditLoginResponse.class.getSimpleName();
    
    @SerializedName("json") private LoginResponse loginResponse;
    
    public LoginResponse getLoginResponse() {
        return loginResponse;
    }

    public static class LoginResponse {
        private List<String[]> errors;
        private LoginData      data;

        public List<String[]> getErrors() {
            return errors;
        }

        public LoginData getData() {
            return data;
        }
    }

    public static class LoginData {
        private String username;
        private String modhash;
        private String cookie;

        public String getUsername() {
            return username;
        }
        
        public void setUsername(String username) {
            this.username = username;
        }
        
        public String getModhash() {
            return modhash;
        }

        public String getCookie() {
            return cookie;
        }
    }

    @Override
    public ContentValues getContentValues() {
        ContentValues values = new ContentValues();
        if (loginResponse.getErrors().size() > 0) {
            StringBuilder errorMessage = new StringBuilder();
            for (String[] error : loginResponse.getErrors())
                errorMessage.append(error[1] + " ");
            
            values.put(RedditContract.Login.SUCCESS, 0);
            values.put(RedditContract.Login.ERROR_MESSAGE, errorMessage.toString());
        } else {
            LoginData data = loginResponse.getData();
            values.put(RedditContract.Login.USERNAME, data.getUsername());
            values.put(RedditContract.Login.COOKIE, data.getCookie());
            values.put(RedditContract.Login.MODHASH, data.getModhash());
            values.put(RedditContract.Login.SUCCESS, 1);
        }
        
        return values;
    }

}

@@ -1496,142 +1496,547 @@
     
-%7D%0A%0A    public static class LoginData %7B%0A        private String username;%0A        private String modhash;%0A        private String cookie;
+    %0A        public void setData(LoginData data) %7B%0A            this.data = data;%0A        %7D%0A        %0A        public void setErrors(List%3CString%5B%5D%3E errors) %7B%0A            this.errors = errors;%0A        %7D%0A    %7D%0A%0A    public static class LoginData %7B%0A        private String username;%0A        private String modhash;%0A        private String cookie;%0A        %0A        public LoginData(String username, String modhash, String cookie) %7B%0A            this.username = username;%0A            this.modhash = modhash;%0A            this.cookie = cookie;%0A        %7D
 %0A%0A  
@@ -2368,32 +2368,274 @@
 okie;%0A        %7D%0A
+        %0A        public boolean isLoggedIn()  %7B%0A            return username != null && !username.equals(%22%22) &&%0A                   modhash  != null && !modhash.equals(%22%22) &&%0A                   cookie   != null && !cookie.equals(%22%22);%0A        %7D%0A
     %7D%0A%0A    @Over

Add utility methods for login data.