content: package com.quizapp.tollywood.screens;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import android.graphics.Color;
  import android.graphics.drawable.ColorDrawable;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.widget.EditText;
  import android.widget.FrameLayout;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  import android.widget.ScrollView;
  
  import com.quizapp.tollywood.AppController;
  import com.quizapp.tollywood.R;
  import com.quizapp.tollywood.Screen;
  import com.quizapp.tollywood.User;
  import com.quizapp.tollywood.adapters.GameEventsListItemAdaptor;
  import com.quizapp.tollywood.adapters.QuizHistoryListAdapter;
  import com.quizapp.tollywood.appcontrollers.ProfileAndChatController;
  import com.quizapp.tollywood.appcontrollers.ProgressiveQuizController;
  import com.quizapp.tollywood.appcontrollers.UserMainPageController;
  import com.quizapp.tollywood.configuration.Config;
  import com.quizapp.tollywood.databaseutils.Badge;
  import com.quizapp.tollywood.databaseutils.GameEvents;
  import com.quizapp.tollywood.databaseutils.LocalQuizHistory;
  import com.quizapp.tollywood.databaseutils.Quiz;
  import com.quizapp.tollywood.datalisteners.DataInputListener;
  import com.quizapp.tollywood.gameutils.GameUtils;
  import com.quizapp.tollywood.uiutils.CircleTransform;
  import com.quizapp.tollywood.uiutils.UiUtils;
  import com.quizapp.tollywood.uiutils.UiUtils.UiText;
  import com.quizapp.tollywood.widgets.BarChartViewMultiDataset;
  import com.quizapp.tollywood.widgets.ExpandableHeightListView;
  import com.quizapp.tollywood.widgets.FlowLayout;
  import com.quizapp.tollywood.widgets.GothamTextView;
  import com.quizapp.tollywood.widgets.PieChartView;
  import com.github.mikephil.charting.data.BarData;
  import com.github.mikephil.charting.data.BarDataSet;
  import com.github.mikephil.charting.data.BarEntry;
  import com.github.mikephil.charting.data.Entry;
  import com.github.mikephil.charting.data.PieData;
  import com.github.mikephil.charting.data.PieDataSet;
  
  public class UserProfileScreen extends Screen {
  	public GothamTextView wonTextView;
  	public GothamTextView lostTextView;
  	public GothamTextView tieTextView;
  	public ScrollView userProfile;
  	private PieChartView mPieChart;
  	private BarChartViewMultiDataset mBarChart;
  	private GothamTextView userName;
  	private ImageView userImage;
  	private GothamTextView userStatusMessage;
  	private GothamTextView userMoreInfo;
  	private AppController controller;
  	private LinearLayout userProfileWrapper;
  	private int win_count = 0 ;
  	private int lose_count = 0;
  	private int tie_count = 0;
  	
  	public UserProfileScreen(AppController cont) {
  		super(cont);
  		controller = cont;
  		screenType = ScreenType.PROFILE_SCREEN;
  	}
  	
  	public void showUser(final User user){
  		userProfile = (ScrollView) LayoutInflater.from(controller.getContext()).inflate(R.layout.user_profile, this, false);
  		userProfileWrapper = (LinearLayout) userProfile.findViewById(R.id.profile_data_wrapper);
  		userProfile.findViewById(R.id.user_info_card).setBackgroundColor(getResources().getColor(R.color.black));
  		userName = (GothamTextView) userProfile.findViewById(R.id.user_card_name);
  		userImage = (ImageView)userProfile.findViewById(R.id.user_card_small_pic);
  		userStatusMessage = (GothamTextView) userProfile.findViewById(R.id.user_status_msg);
  		userMoreInfo = (GothamTextView) userProfile.findViewById(R.id.user_more_info);
  		wonTextView = (GothamTextView) userProfile.findViewById(R.id.win_count);
  		lostTextView = (GothamTextView) userProfile.findViewById(R.id.lose_count);
  		tieTextView = (GothamTextView) userProfile.findViewById(R.id.tie_count);
  		mPieChart = (PieChartView) userProfile.findViewById(R.id.pie_chart);
  		mBarChart = (BarChartViewMultiDataset) userProfile.findViewById(R.id.bar_chart);
   //       setSampleData(controller.getContext());
  
  		addView(userProfile);
  		FlowLayout badgesLayout = (FlowLayout) userProfile.findViewById(R.id.userbadges);
  		userName.setText(user.getName());
  		getApp().getUiUtils().loadImageIntoView(getApp().getContext(), userImage, user.pictureUrl, false, new CircleTransform());
  
  		userName.setOnClickListener(new OnClickListener() {
  			@Override
  			public void onClick(View view) {
  				if(!getApp().getUser().uid.equalsIgnoreCase(user.uid))
  					getApp().getStaticPopupDialogBoxes().showUserSelectedMenu(user, new DataInputListener<Integer>(){
  						@Override
  						public String onData(Integer s) {
  							switch (s){
  							case 2:
  								((ProfileAndChatController)controller).loadChatScreen(user, -1);
  							break;
  							case 3:
  							((UserMainPageController)getApp().loadAppController(UserMainPageController.class)).showAllUserQuizzes(UiText.SELECT_TO_CHALLENGE_USER.getValue(user.getName()), new DataInputListener<Quiz>(){
  								public String onData(Quiz quiz) {
  									((ProgressiveQuizController)getApp().loadAppController(ProgressiveQuizController.class)).startNewChallenge(user, quiz);
  									return null;
  								};
  							});
  							break;
  
  							case 4:
  							getApp().getDataBaseHelper().toggleSubscription(user, new DataInputListener<Boolean>(){
  								@Override
  								public String onData(Boolean s) {
  									return null;
  								}
  							});
  							break;
  						}
  						return null;
  						}
  
  					});
  			}
  		});
  		userStatusMessage.setText(user.getStatus());
  		userMoreInfo.setText(user.place);
  		
  		ArrayList<String>badgeIds = user.getBadges();
  		for(String badgeId : badgeIds){
  			Badge badge = getApp().getDataBaseHelper().getBadgeById(badgeId);
  			ImageView temp = new ImageView(getApp().getContext());
  			temp.setPadding(0,0,20,10);
  			badgesLayout.addView(temp);
  			getApp().getUiUtils().loadImageIntoView(getApp().getContext(), temp, badge.getAssetPath(), true , getApp().getUiUtils().dp2px(20),getApp().getUiUtils().dp2px(20), null);
  			temp.setTag(badgeId);
  			temp.setOnClickListener(new OnClickListener() {
  				@Override
  				public void onClick(View v) {
  					getApp().getStaticPopupDialogBoxes().showUnlockedBadge((String)v.getTag(), true);
  				}
  			});
  		}
  		
  		drawUserQuizChartsAndUpdateStats(user);
  	}
  	
  	
  	public void addEventsListView(List<GameEvents> events){
  		final GameEventsListItemAdaptor gameEventsAdapter = new GameEventsListItemAdaptor(getApp(), 0, events);
  		LinearLayout lView = (LinearLayout) getApp().getActivity().getLayoutInflater().inflate(R.layout.block_list_view, this, false);
   		lView.setBackgroundColor(getResources().getColor(R.color.translucent_white));
  		EditText searchText = (EditText) lView.findViewById(R.id.search_text);
  		FrameLayout viewMore = (FrameLayout) lView.findViewById(R.id.view_all_wrapper);
  		GothamTextView debugMessage = (GothamTextView) lView.findViewById(R.id.debugMessage);
  		if(events.size()==0){
  			debugMessage.setVisibility(View.VISIBLE);
  			debugMessage.setText(UiText.NO_ACTIVITY_AVAILABLE.getValue());
  		}
  		searchText.setVisibility(View.GONE);//hide search
  		GothamTextView titleView = (GothamTextView) lView.findViewById(R.id.title_text_view);
  		titleView.setTextColor(Color.BLACK);
  		titleView.setText(UiText.ACTIVITY_LOG.getValue());
  		ExpandableHeightListView listView = (ExpandableHeightListView) lView.findViewById(R.id.listView);
  		listView.setDivider(new ColorDrawable(this.getResources().getColor(R.color.translucent_black)));
  		listView.setDividerHeight(1);
  
  		LayoutParams lParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  		lParams.setMargins(5, 0, 0, 5);
  		listView.setLayoutParams(lParams);
  		listView.setAdapter(gameEventsAdapter);
  	//	addListenersToQuizListItem(listView);
  		listView.setExpanded(true);
  		if(userProfileWrapper!=null){
  			viewMore.setVisibility(View.GONE);
  			userProfileWrapper.addView(lView );
  		}
  		else{
  			addView(lView);
  		}
  	}
  
  	private void drawUserQuizChartsAndUpdateStats(User user){
  		win_count = 0;
  		lose_count = 0;
  		tie_count = 0;
  		List<Quiz> quizList = getApp().getDataBaseHelper().getAllQuizzesOrderedByXP();
  		ArrayList<Entry> yVals1 = new ArrayList<Entry>();
  		ArrayList<BarEntry> yValsWins = new ArrayList<BarEntry>();
  		ArrayList<BarEntry> yValsLosses = new ArrayList<BarEntry>();
  		ArrayList<BarEntry> yValsTies = new ArrayList<BarEntry>();
  		ArrayList<String> xVals = new ArrayList<String>();
  		int sz = quizList.size();
  
  		int[] winsLosses;
  		List<String> qIdList = new ArrayList<String>();
  		double userXp = 0;
  		double cuserXp = 0;
  		double maxuserXP = 0;
  		int myindex = 0;
  		for (int i = 0; i < sz; i++) {
  			cuserXp = user.getPoints(quizList.get(i).quizId);
  			if (maxuserXP < cuserXp)
  				maxuserXP = cuserXp;
  			if (i<Config.PIE_CHART_MAX_FIELDS-1 && cuserXp/maxuserXP>0.01){
  				userXp = cuserXp;
  				qIdList.add(quizList.get(i).quizId);
  				xVals.add(GameUtils.reduceString(quizList.get(i).name));
  				myindex = i;
  			}
  			else{
  				userXp = userXp + cuserXp;
  				qIdList.add(((Quiz)quizList.get(i)).quizId);
  				if(i!=sz-1)
  					continue;
  				xVals.add(UiUtils.UiText.PIE_CHART_OTHERS_TEXT.getValue());
  				myindex = Config.PIE_CHART_MAX_FIELDS-1;
  			}
  			winsLosses = user.getWinsLossesSum(qIdList);
  //            yVals1.add(new Entry((float)getApp().getGameUtils().getLevelFromXp(userXp), myindex));
              yVals1.add(new Entry((float)userXp, myindex));
              // update win lose tie counts here
              win_count = win_count + winsLosses[0];
              lose_count = lose_count + winsLosses[1];
              tie_count = tie_count + winsLosses[2];
              yValsWins.add(new BarEntry(winsLosses[0],myindex));
              yValsLosses.add(new BarEntry(winsLosses[1],myindex));
              yValsTies.add(new BarEntry(winsLosses[2],myindex));
              qIdList.clear();
              userXp = 0;
          }
  		
  		//update stats
  		wonTextView.setText(win_count+" "+UiUtils.UiText.PROFILE_WON_STATS_TEXT.getValue());
  		lostTextView.setText(lose_count+" "+UiUtils.UiText.PROFILE_LOST_STATS_TEXT.getValue());
  		tieTextView.setText(tie_count+" "+UiUtils.UiText.PROFILE_TIE_STATS_TEXT.getValue());
  		
  		// TODO: Reduce code redundancy by checking for nonzero values in a method (present in uiutils, conflict for BarEntry and Entry)
  		boolean nonZeroFlag = false;
  		for(int i=0;i<yVals1.size();i++){
  			if(yVals1.get(i).getVal() > 0){
  				nonZeroFlag = true;
  			}
  		}
  		if(nonZeroFlag)
  			drawUserActivityDistributionChart(xVals,yVals1);
  		nonZeroFlag = false;
  		for(int i=0;i<yVals1.size();i++){
  			if(yValsWins.get(i).getVal() > 0 || yValsLosses.get(i).getVal() > 0 || yValsTies.get(i).getVal() > 0 ){
  				nonZeroFlag = true;
  			}
  		}
  		if(nonZeroFlag)
  			drawWinLoseTieChart(xVals,yValsWins,yValsLosses,yValsTies);
  	}
  	
  //	public void drawUserCategoryChartsAndUpdateStats(User user){
  //		int win_count = 0;
  //		int lose_count = 0;
  //		int tie_count = 0;
  //		List<Category> categories = getApp().getDataBaseHelper().getAllCategories();
  //		ArrayList<Entry> yVals1 = new ArrayList<Entry>();
  //		ArrayList<BarEntry> yValsWins = new ArrayList<BarEntry>();
  //		ArrayList<BarEntry> yValsLosses = new ArrayList<BarEntry>();
  //		ArrayList<BarEntry> yValsTies = new ArrayList<BarEntry>();
  //		ArrayList<String> xVals = new ArrayList<String>();
  //		int sz = categories.size();
  //		
  //		for(int i=0;i<sz;i++){
  //			xVals.add(categories.get(i).shortDescription);
  //		}
  //		int[] winsLosses;
  //		List<String> qIdList = new ArrayList<String>();
  //		List<Quiz> qList = null;
  //		for (int i = 0; i < sz; i++) {
  //			qList = categories.get(i).getQuizzes(getApp());
  //			qIdList.clear();
  //			float totalXP = 0;
  //			for(int j=0;j<qList.size();j++){
  //				totalXP = totalXP + (float)user.getPoints(qList.get(j).quizId);
  //				qIdList.add(((Quiz)qList.get(i)).quizId);
  //			}
  //			winsLosses = user.getWinsLossesSum(qIdList);
  //            yVals1.add(new Entry((float)getApp().getGameUtils().getLevelFromXp(totalXP), i));
  //            // update win lose tie counts here
  //            yValsWins.add(new BarEntry(winsLosses[0],i));
  //            yValsLosses.add(new BarEntry(winsLosses[1],i));
  //            yValsTies.add(new BarEntry(winsLosses[2],i));
  //        }
  //		
  //		//update stats
  //		wonTextView.setText(win_count+" "+UiUtils.UiText.PROFILE_WON_STATS_TEXT.getValue());
  //		lostTextView.setText(lose_count+" "+UiUtils.UiText.PROFILE_LOST_STATS_TEXT.getValue());
  //		tieTextView.setText(tie_count+" "+UiUtils.UiText.PROFILE_TIE_STATS_TEXT.getValue());
  //		
  //		// TODO: Reduce code redundancy by checking for nonzero values in a method (present in uiutils, conflict for BarEntry and Entry)
  //		boolean nonZeroFlag = false;
  //		for(int i=0;i<yVals1.size();i++){
  //			if(yVals1.get(i).getVal() > 0){
  //				nonZeroFlag = true;
  //			}
  //		}
  //		if(nonZeroFlag)
  //			drawUserActivityDistributionChart(xVals,yVals1);
  //		nonZeroFlag = false;
  //		for(int i=0;i<yVals1.size();i++){
  //			if(yValsWins.get(i).getVal() > 0 || yValsLosses.get(i).getVal() > 0 || yValsTies.get(i).getVal() > 0 ){
  //				nonZeroFlag = true;
  //			}
  //		}
  //		if(nonZeroFlag)
  //			drawCategoryWiseLevelsChart(xVals,yValsWins,yValsLosses,yValsTies);
  //	}
  	
  	private void drawWinLoseTieChart(ArrayList<String> xVals,ArrayList<BarEntry> yValsWins,ArrayList<BarEntry> yValsLosses,ArrayList<BarEntry> yValsTies){
  	        ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
  	        BarDataSet set;
  
  			set = new BarDataSet(yValsWins, "Won");
  			set.setColor(this.getApp().getConfig().getAThemeColor());
  			dataSets.add(set);
  			set = new BarDataSet(yValsLosses, "Lost");
  			set.setColor(this.getApp().getConfig().getAThemeColor());
  			dataSets.add(set);
  			set = new BarDataSet(yValsTies, "Tie");
  			set.setColor(this.getApp().getConfig().getAThemeColor());
  			dataSets.add(set);
  			
  			BarData data = new BarData(xVals, dataSets);
  //			data.setGroupSpace(5f);
  
  	        mBarChart.setData(data);
  	        mBarChart.setDescriptionTextSize(6f);
  	        mBarChart.setValueTextSize(6f);
  	        mBarChart.setDescription("Quiz Stats");
  	        mBarChart.invalidate();
  	}
  	
  	private void drawUserActivityDistributionChart(ArrayList<String> xVals,ArrayList<Entry> yVals){
  	
  		PieDataSet set = new PieDataSet(yVals, "Quiz Stats");
  		set.setSliceSpace(3f);
  		set.setColors(Config.themeColors);
  //        set1.setColors(ColorTemplate.createColors(controller.getContext().getApplicationContext(),ColorTemplate.VORDIPLOM_COLORS));
          PieData data = new PieData(xVals, set);
          mPieChart.setValueFormatter(getApp().getUiUtils().getDecimalFormatter());
          mPieChart.setData(data);
          mPieChart.setDescriptionTextSize(6f);
          mPieChart.setValueTextSize(7f);
  
          // undo all highlights
          mPieChart.highlightValues(null);
  
          // set a text for the chart center
          mPieChart.setCenterText("Total XP: " + (int) mPieChart.getYValueSum());
          
          mPieChart.setDescription(UiText.TOTAL_GAMES_PLAYED.getValue(win_count+lose_count+tie_count));
          mPieChart.invalidate();
  	}
  	
  //	// TODO : below method must be remove at the end
  //	public void setSampleData(Context ctxt){
  //		int types = 4;
  //		float scale = 4;
  //		String[] mParties = new String[] {"Quiz1", "Quiz2", "Quiz3", "Quiz4"};
  //		
  //        float mult = (float) scale;
  //
  //        ArrayList<Entry> yVals1 = new ArrayList<Entry>();
  //        // ArrayList<Entry> yVals2 = new ArrayList<Entry>();
  //
  //        // IMPORTANT: In a PieChart, no values (Entry) should have the same
  //        // xIndex (even if from different DataSets), since no values can be
  //        // drawn above each other.
  //        for (int i = 0; i < types + 1; i++) {
  //            yVals1.add(new Entry((float) (Math.random() * mult) + mult / 5, i));
  //        }
  //
  //        // for (int i = types / 2; i <
  //        // types; i++) {
  //        // yVals2.add(new Entry((float) (Math.random() * mult) + mult / 5, i));
  //        // }
  //
  //        ArrayList<String> xVals = new ArrayList<String>();
  //
  //        for (int i = 0; i < types + 1; i++)
  //            xVals.add(mParties[i % mParties.length]);
  //
  //        PieDataSet set1 = new PieDataSet(yVals1, "Quiz Stats");
  //        set1.setSliceSpace(3f);
  //        set1.setColors(ColorTemplate.createColors(ctxt.getApplicationContext(),
  //                ColorTemplate.VORDIPLOM_COLORS));
  //
  //        PieData data = new PieData(xVals, set1);
  //        mPieChart.setData(data);
  //
  //        // undo all highlights
  //        mPieChart.highlightValues(null);
  //
  //        // set a text for the chart center
  //        mPieChart.setCenterText("Total Value\n" + (int) mPieChart.getYValueSum() + "\n(all slices)");
  //        mPieChart.invalidate();
  //	 }
  	
  	public void addInfoLocalSummary(){
  		
  	}
  
  	public void showHistoryWithUser(User user , List<LocalQuizHistory> history) {
  		int wins = 0;
  		int lose = 0;
  		for(LocalQuizHistory l : history){
  			wins+= l.getQuizResult()==Quiz.WON? 1 : 0; 
  			lose+= l.getQuizResult()==Quiz.LOOSE? 1 : 0; 
  		}
  		LinearLayout winLoseStrip = (LinearLayout) getApp().getActivity().getLayoutInflater().inflate(R.layout.quiz_wins_lose_strip, null);
  		winLoseStrip.setBackgroundColor(getApp().getConfig().getAThemeColor());
  		((GothamTextView) winLoseStrip.findViewById(R.id.title_text_view)).setText(UiText.YOU_VS_USER.getValue(user.getName()));
  		GothamTextView debugMessage = (GothamTextView) winLoseStrip.findViewById(R.id.debugMessage);
  		debugMessage.setTextSize(25);
  		debugMessage.setText(wins+"-"+lose);
  		if(userProfileWrapper!=null)
  			userProfileWrapper.addView(winLoseStrip);
  		else
  			addToScrollView(winLoseStrip);
  		showHistory(history, true);
  	}
  	
  	/**
  	 * 
  	 * @param history
  	 * @param isAdddedToScroller
  	 * 
  	 * quizApp.getDataBaseHelper().getAllUsersByUid(QuizHistoryListAdapter.getAllUids(history), new DataInputListener<Boolean>(){
  					@Override
  					public String onData(Boolean s) {
  					return super.onData(s);
  				}
  			});
  	 */
  	public void showHistory(List<LocalQuizHistory> history , boolean isAdddedToScroller){
  		final QuizHistoryListAdapter quizHistoryAdapter = new QuizHistoryListAdapter(getApp(), 0, history);
  		LinearLayout lView = (LinearLayout) getApp().getActivity().getLayoutInflater().inflate(R.layout.block_list_view, this, false);
   		lView.setBackgroundColor(getResources().getColor(R.color.translucent_white));
  		EditText searchText = (EditText) lView.findViewById(R.id.search_text);
  		
  		GothamTextView debugMessage = (GothamTextView) lView.findViewById(R.id.debugMessage);
  		if(history.size()==0){ 
  			debugMessage.setVisibility(View.VISIBLE);
  			debugMessage.setText(UiText.NO_ACTIVITY_AVAILABLE.getValue());
  		}
  		searchText.setVisibility(View.GONE);//hide search
  		GothamTextView titleView = (GothamTextView) lView.findViewById(R.id.title_text_view);
  		titleView.setText(UiText.LOCAL_QUIZ_HISTORY.getValue());
  		titleView.setTextColor(Color.GRAY);
  		ExpandableHeightListView listView = (ExpandableHeightListView) lView.findViewById(R.id.listView);
  		listView.setDivider(new ColorDrawable(this.getResources().getColor(R.color.translucent_black)));
  		listView.setDividerHeight(1);
  
  		LayoutParams lParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  		lParams.setMargins(5, 0, 0, 5);
  		listView.setLayoutParams(lParams);
  		listView.setAdapter(quizHistoryAdapter);
  	//	addListenersToQuizListItem(listView);
  		if(isAdddedToScroller){
  			listView.setExpanded(true);
  			if(userProfileWrapper!=null){
  				FrameLayout viewMore = (FrameLayout) lView.findViewById(R.id.view_all_wrapper);
  				viewMore.setVisibility(View.GONE);
  				userProfileWrapper.addView(lView);
  			}
  			else
  				addToScrollView(lView );
  		}
  		else{
  			addView(lView);
  			FrameLayout viewMore = (FrameLayout) lView.findViewById(R.id.view_all_wrapper);
  			viewMore.setVisibility(View.GONE);
  
  		}
  	}	
  	@Override
  	public boolean showMenu() {
  		// TODO Auto-generated method stub
  		return true;
  	}
  
  	public ScreenType getScreenType(){
  		return ScreenType.PROFILE_SCREEN;
  	}
  }
  