package com.dubiousoft.sakana;

import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Pair;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;

public class WeaponSelectionView extends FrameLayout 
	implements OnClickListener {

	public enum WeaponAttribute {
		MASS( "Mass" ),
		SPEED( "Speed" ),
		PROJECTILE_COUNT( "ProjectileCount" );
		
		private final String m_stringRep;
		
		private WeaponAttribute( String rep ) {
			m_stringRep = rep;
		}
		
		public String toString() {
			return m_stringRep;
		}
		
		public int iconResource() {
			if (m_stringRep.equals( MASS.toString() )) {
				return R.drawable.mass;
			}
			else if (m_stringRep.equals( SPEED.toString() )) {
				return R.drawable.velocity;
			}
			else if (m_stringRep.equals( PROJECTILE_COUNT.toString() )) {
				return R.drawable.count;
			}
			throw new RuntimeException( "iconResource parse error: " + m_stringRep );			
		}
	}

	private static class WeaponInfo {
		WeaponAttribute top;
		int topIconFile;
		WeaponAttribute bottom;
		int bottomIconFile;
	};
	
	private NativeSakana m_nativeLayer;
	private Vector<WeaponInfo> m_weapons = new Vector<WeaponInfo>();	
	private SliderView m_slider;
	private final int ICON_SIZE = 100;
	private final int PADDING = 10;
	
	public WeaponSelectionView(final Context context) {
        this(context, null);
    }

    public WeaponSelectionView(final Context context, final AttributeSet attrs) {
        this(context, attrs, R.style.AppTheme);
    }

    public WeaponSelectionView(final Context context, final AttributeSet attrs,
            final int defStyle) {
        super(context, attrs, defStyle);
    }
    
    public void setNativeLayer( NativeSakana nativeLayer ) {
    	m_nativeLayer = nativeLayer;
    }
    
    public void setSlider( SliderView slider ) {
    	m_slider = slider;
    }

    public void readWeapon( String path ) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder db;
			db = dbf.newDocumentBuilder();
			Document doc = db.parse(getContext().getAssets().open( path ));
			doc.normalize();
			Utils.cleanNode( doc );
			
			Element weaponDescription = (Element) doc.getFirstChild();
			Node iconNode = Utils.getChildNode( weaponDescription, "Icon" );
			String iconFile = iconNode.getTextContent();
			WeaponInfo weaponInfo = readWeaponInfo( weaponDescription );

			ImageView selectionSquare = (ImageView)findViewById( R.id.selection_square );
			selectionSquare.setLayoutParams( new LayoutParams( ICON_SIZE, ICON_SIZE ) );
			selectionSquare.setY( 0 );

			ImageView iconView = new ImageView( getContext() );
			InputStream stream = getContext().getAssets().open( "Weapons/" + iconFile );
			iconView.setImageBitmap( BitmapFactory.decodeStream( stream ) );
			iconView.setX( 0 );
			iconView.setY( m_weapons.size() * (ICON_SIZE+PADDING) );
			iconView.setOnClickListener( this );
			iconView.setId( m_weapons.size() );
			addView( iconView, new LayoutParams( ICON_SIZE, ICON_SIZE ) );
			m_weapons.add( weaponInfo );
			setLayoutParams( new LayoutParams( ICON_SIZE, m_weapons.size()*(ICON_SIZE+PADDING) ) );
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    private Pair<Integer,Integer> upAndDown( Node node ){
    	Node upNode = Utils.getChildNode( node, "Up" );
    	Node downNode = Utils.getChildNode( node, "Down" );
    	int up = Integer.parseInt( upNode.getTextContent() );
    	int down = Integer.parseInt( downNode.getTextContent() );
    	return new Pair<Integer,Integer>( up, down );
    }
    
    private void assignInfo( WeaponInfo info, Node node, WeaponAttribute attr ) {
		Pair<Integer,Integer> upDown = upAndDown( node );
		if (upDown.first < upDown.second) {
			info.bottom = attr;
			info.bottomIconFile = attr.iconResource();
		}
		else if (upDown.first > upDown.second) {
			info.top = attr;
			info.topIconFile = attr.iconResource();
		}    	
    }
    
    private WeaponInfo readWeaponInfo( Element node ) {
    	WeaponInfo info = new WeaponInfo();    			
    	Node massNode = Utils.getChildNode( node, "Mass" );
    	assignInfo( info, massNode, WeaponAttribute.MASS );

    	Node speedNode = Utils.getChildNode( node, "Speed" );
    	assignInfo( info, speedNode, WeaponAttribute.SPEED );
    	
    	Node countNode = Utils.getChildNode( node, "ProjectileCount" );
    	assignInfo( info, countNode, WeaponAttribute.PROJECTILE_COUNT );
    	
    	return info;
    }
    
    @Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		((Activity)getContext()).getLayoutInflater().inflate(R.layout.weapon_selection_view, this);
	}

	@Override
	public void onClick(View view) {
		int weaponIndex = view.getId();
		ImageView selectionSquare = (ImageView)findViewById( R.id.selection_square );
		LayoutParams params = new LayoutParams( ICON_SIZE, ICON_SIZE );
		params.topMargin = weaponIndex * (ICON_SIZE+PADDING);
		selectionSquare.setLayoutParams( params );
		m_nativeLayer.SelectWeapon( weaponIndex );
		WeaponInfo info = m_weapons.get( weaponIndex );
		m_slider.setTopIcon( info.topIconFile );
		m_slider.setBottomIcon( info.bottomIconFile );
	}
    
}
