package com.adsdk.sdk.nativeads;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("ViewConstructor")
public class NativeAdView extends FrameLayout {
	private Context context;
	private View adView;

	public NativeAdView(Context context, NativeAd ad, NativeViewBinder binder) {
		super(context);
		this.context = context;
		adView = inflate(context, binder.getAdLayoutId(), null);
		fillAdView(ad, binder);
		this.addView(adView);
		// TODO: click listeners
		// TODO: impression tracking 
	}

	public void fillAdView(NativeAd ad, NativeViewBinder binder) {
		for (String key : binder.getTextAssetsBindingsKeySet()) {
			int resId = binder.getIdForTextAsset(key);
			if (resId == 0) {
				continue;				
			}
			TextView view = (TextView)adView.findViewById(resId); //TODO: exception handling
			String text = ad.getTextAsset(key);
			if(view != null && text != null) {
				view.setText(text); 
			}
		}

		for (String key : binder.getImageAssetsBindingsKeySet()) {
			int resId = binder.getIdForImageAsset(key);
			if (resId == 0) {
				continue;				
			}
			ImageView view = (ImageView)adView.findViewById(resId); //TODO: exception handling
			Bitmap imageBitmap = ad.getImageAsset(key).bitmap;
			if(view != null && imageBitmap != null) {
				view.setImageBitmap(imageBitmap);
			}
		}
		
	}

}

@@ -247,24 +247,84 @@
 .widget.
-TextView
+RatingBar;%0Aimport android.widget.TextView;%0A%0Aimport com.adsdk.sdk.Log
 ;%0A%0A@Supp
@@ -736,17 +736,16 @@
 tracking
- 
 %0A%09%7D%0A%0A%09pu
@@ -949,102 +949,399 @@
 nue;
+%0A
 %09%09%09
-%09
+%7D
 %0A%09%09%09
-%7D%0A%09%09%09TextView view = (TextView)adView.findViewById(resId); //TODO: exception handling%0A
+try %7B%0A%09%09%09%09if (key.equals(%22rating%22)) %7B // rating is special, not displayed as normal text view.%0A%09%09%09%09%09RatingBar bar = (RatingBar) adView.findViewById(resId);%0A%09%09%09%09%09if (bar != null) %7B%0A%09%09%09%09%09%09int rating = Integer.parseInt(ad.getTextAsset(key));%0A%09%09%09%09%09%09bar.setIsIndicator(true);%0A%09%09%09%09%09%09bar.setRating(rating);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09TextView view = (TextView) adView.findViewById(resId);%0A%09%09
 %09%09%09S
@@ -1374,26 +1374,29 @@
 et(key);%0A%09%09%09
+%09%09
 if
+ 
 (view != nul
@@ -1412,32 +1412,34 @@
  != null) %7B%0A%09%09%09%09
+%09%09
 view.setText(tex
@@ -1441,17 +1441,107 @@
 t(text);
- 
+%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D catch (ClassCastException e) %7B%0A%09%09%09%09Log.e(%22Cannot fill view for %22 + key);
 %0A%09%09%09%7D%0A%09%09
@@ -1689,18 +1689,24 @@
 nue;
-%09%09%09%09
 %0A%09%09%09%7D%0A
+%09%09%09try %7B%0A%09
 %09%09%09I
@@ -1732,16 +1732,17 @@
 ageView)
+ 
 adView.f
@@ -1765,35 +1765,10 @@
 d); 
-//TODO: exception handling
 %0A
+%09
 %09%09%09B
@@ -1820,18 +1820,20 @@
 map;%0A%09%09%09
+%09
 if
+ 
 (view !=
@@ -1864,24 +1864,25 @@
 null) %7B%0A%09%09%09%09
+%09
 view.setImag
@@ -1903,16 +1903,100 @@
 itmap);%0A
+%09%09%09%09%7D%0A%09%09%09%7D catch (ClassCastException e) %7B%0A%09%09%09%09Log.e(%22Cannot fill view for %22 + key);%0A
 %09%09%09%7D%0A%09%09%7D
@@ -1996,17 +1996,15 @@
 %09%09%7D%0A%09%09%7D%0A
-%09%09
 %0A%09%7D%0A%0A%7D%0A

Add rating bar in native ads.