package com.example.lamano;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/*
 * Clase del Fragment que despliega un perfil de un Amigo 
 */
public class AmigoFragment extends Fragment{
	
	String name, cumple, fono, dir, email;
	
	@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View toreturn = inflater.inflate(R.layout.friend_view, container, false);
        initList();
        updateData(toreturn);
        return toreturn;
    }
	
	/* Método para definir nombre del amigo a buscar */
	public void setName(String n){
		name = n;
		
	}
	
	/* Método que guarda la información del Amigo */
	private void setData(String n, String c, String t, String d, String e){
		name = n;
		cumple = c;
		fono = t;
		dir = d;
		email = e;
	}
	
	/* Método que agrega la información al view */
	private void updateData(View v){
		TextView name_tv = (TextView) v.findViewById(R.id.Txt_User_Name);
        name_tv.setText(name);
        TextView cumple_tv = (TextView) v.findViewById(R.id.Txt_User_Cumple);
        cumple_tv.setText(cumple);
        TextView fono_tv = (TextView) v.findViewById(R.id.Txt_User_phone);
        fono_tv.setText(fono);
        TextView dir_tv = (TextView) v.findViewById(R.id.Txt_User_address);
        dir_tv.setText(dir);
        TextView email_tv = (TextView) v.findViewById(R.id.Txt_User_M_address);
        email_tv.setText(email);
	}
	
	/* Método que inicializa el xml con los datos */
	private void initList(){
		XmlPullParserFactory pullParserFactory;
		try {
			pullParserFactory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = pullParserFactory.newPullParser();

			    InputStream in_s = this.getActivity().getApplicationContext().getAssets().open("frienddata.xml");
		        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
	            parser.setInput(in_s, null);

	            parseXML(parser);
	            in_s.close();

		} catch (XmlPullParserException e) {

			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/* Método que recorre el xml en busca de los datos */
	private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
	{
        int eventType = parser.getEventType();
        String nombre="ERR", cumple="ERR", telefono="ERR", dir="ERR",email="ERR" ;
        while (eventType != XmlPullParser.END_DOCUMENT){
            String name = null;
            switch (eventType){
                case XmlPullParser.START_DOCUMENT:
        
                    break;
                case XmlPullParser.START_TAG:
                    name = parser.getName();
                    if(name.equalsIgnoreCase("nombre"))
                		nombre = parser.nextText();
                    else if(name.equalsIgnoreCase("cumpleaños"))
                    	cumple = parser.nextText();
                    else if(name.equalsIgnoreCase("telefono"))
                    	telefono = parser.nextText();
                    else if(name.equalsIgnoreCase("direccion"))
                    	dir = parser.nextText();
                    else if(name.equalsIgnoreCase("email"))
                    	email = parser.nextText();
                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase("frienddata") && nombre.equals(this.name)){
                    	setData(nombre,cumple,telefono,dir,email);
                    	return;
                    } 
            }
            eventType = parser.next();
        }

	}
}
