package com.runner.sportsmeter.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.facebook.AccessToken;
import com.facebook.login.widget.ProfilePictureView;
import com.google.android.gms.ads.AdView;
import com.runner.sportsmeter.R;
import com.runner.sportsmeter.common.Utility;
import com.runner.sportsmeter.models.Account;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;

import java.util.HashMap;

/**
 * Created by angelr on 03-Sep-15.
 */
public class AccountActivity extends Activity {
    private final HashMap accounts = new HashMap();
    ProfilePictureView profilePic;
//    ProgressBar progress;
    TextView name, userName, eMail, createdAt, isVerified;
    Button closeBtn;
    AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_layout);

        // init views
//        progress = (ProgressBar) findViewById(R.id.progress_bar);

        profilePic = (ProfilePictureView) findViewById(R.id.profile_picture);
        name = (TextView) findViewById(R.id.edit_name);
        userName = (TextView) findViewById(R.id.edit_username);
        eMail = (TextView) findViewById(R.id.edit_mail);
        isVerified = (TextView) findViewById(R.id.edit_is_verified);
        createdAt = (TextView) findViewById(R.id.edit_date);
        closeBtn = (Button) findViewById(R.id.close_btn);

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // todo save account hashmap to local datastore
        if(!accounts.containsKey(ParseUser.getCurrentUser().getUsername())) {
            accounts.put(ParseUser.getCurrentUser().getUsername(), convertFromUserToAccount(ParseUser.getCurrentUser()));
        }

        Account current = (Account) accounts.get(ParseUser.getCurrentUser().getUsername());

        if(ParseFacebookUtils.isLinked(ParseUser.getCurrentUser())){
            String facebookId = AccessToken.getCurrentAccessToken().getUserId();
            profilePic.setProfileId(facebookId);
        }
        name.setText(current.getName());
        userName.setText(current.getName());
        eMail.setText(current.getEmail());
        isVerified.setText(current.getIsVerified().toString());
        createdAt.setText(current.getCreatedAt());

        // setup adds
        mAdView = (AdView) findViewById(R.id.adViewAccount);
        new Utility().setupAdds(mAdView, this);
    }

    private Account convertFromUserToAccount(ParseUser currentUser) {
        return new Account(currentUser.getUsername(),
                currentUser.get("name").toString(),
                currentUser.getEmail(),
                (Boolean)currentUser.get("emailVerified"),
                Utility.formatDate(currentUser.getCreatedAt()));
    }
}

@@ -140,17 +140,54 @@
 .widget.
-*
+Button;%0Aimport android.widget.TextView
 ;%0Aimport
@@ -237,81 +237,283 @@
 ook.
-login.widget.ProfilePictureView;%0Aimport com.google.android.gms.ads.AdView
+GraphRequest;%0Aimport com.facebook.GraphResponse;%0Aimport com.facebook.HttpMethod;%0Aimport com.facebook.login.widget.ProfilePictureView;%0Aimport com.google.android.gms.ads.AdView;%0Aimport com.parse.ParseFacebookUtils;%0Aimport com.parse.ParseTwitterUtils;%0Aimport com.parse.ParseUser
 ;%0Aim
@@ -638,73 +638,8 @@
 unt;
-%0Aimport com.parse.ParseFacebookUtils;%0Aimport com.parse.ParseUser;
 %0A%0Aim
@@ -968,16 +968,39 @@
 mAdView;
+%0A    String facebookId;
 %0A%0A    @O
@@ -2312,31 +2312,24 @@
             
-String 
 facebookId =
@@ -2515,32 +2515,733 @@
 ent.getName());%0A
+        // todo get facebook email%0A        if(ParseFacebookUtils.isLinked(ParseUser.getCurrentUser()))%7B%0A            new GraphRequest(%0A                    AccessToken.getCurrentAccessToken(),%0A                    facebookId,%0A                    null,%0A                    HttpMethod.GET,%0A                    new GraphRequest.Callback() %7B%0A                        public void onCompleted(GraphResponse response) %7B%0A                            eMail.setText(response.getRawResponse());%0A                        %7D%0A                    %7D%0A            ).executeAsync();%0A        %7D else if(ParseTwitterUtils.isLinked(ParseUser.getCurrentUser())) %7B%0A            eMail.setText(%22twitter@sdfg.com%22);%0A        %7D else %7B%0A    
         eMail.se
@@ -3263,24 +3263,34 @@
 etEmail());%0A
+        %7D%0A
         isVe

add graph request to get email from facebook users