package de.tudarmstadt.informatik.hostage.ui2.adapter;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import java.util.List;

import de.tudarmstadt.informatik.hostage.R;
import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
import de.tudarmstadt.informatik.hostage.ui2.fragment.ServicesFragment;
import de.tudarmstadt.informatik.hostage.ui2.model.ServicesListItem;

/**
 * Created by Daniel Lazar on 06.02.14.
 */
public class ServicesListAdapter extends ArrayAdapter<ServicesListItem> {

	private SharedPreferences mConnectionInfo;

	private class ViewHolder {
			public TextView protocolName;
			public TextView recordedAttacks;
			public Switch activated;
			public View circle;
		}

	private Context mActivity;
	private Switch mServicesSwitch;
	private CompoundButton.OnCheckedChangeListener mListener;
	private final Context context;
	private final List<ServicesListItem> values;
	int sdk = Build.VERSION.SDK_INT;

	public ServicesListAdapter(Context context, List<ServicesListItem> objects){
		super(context, R.layout.services_list_item, objects);

		this.context = context;
		this.values = objects;
	}

	public void setActivity(Context activity, Switch servicesSwitch, CompoundButton.OnCheckedChangeListener mainListener) {
		mActivity = activity;
		mServicesSwitch = servicesSwitch;
		mListener = mainListener;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = convertView;

		ViewHolder holder = null;

		final ServicesListItem item = values.get(position);

		if(rowView == null){
			rowView = inflater.inflate(R.layout.services_list_item, parent, false);

			holder = new ViewHolder();
			holder.protocolName = (TextView) rowView.findViewById(R.id.services_item_name);
			holder.recordedAttacks = (TextView) rowView.findViewById(R.id.services_item_rec_attacks);
			holder.activated = (Switch) rowView.findViewById(R.id.services_item_switch);
			holder.circle = rowView.findViewById(R.id.services_circle);
			rowView.setTag(holder);

		} else {
			holder = (ViewHolder) rowView.getTag();
		}

		holder.protocolName.setText(item.protocol);
		holder.activated.setTag(item);

		this.updateStatus(item, holder);

			holder.activated.setOnCheckedChangeListener(
					new CompoundButton.OnCheckedChangeListener() {
						public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
							ServicesListItem item = (ServicesListItem) buttonView.getTag();
				if(!HelperUtils.isWifiConnected(mActivity)){
					new AlertDialog.Builder(mActivity)
							.setTitle(R.string.information)
							.setMessage(R.string.wifi_not_connected_msg)
							.setPositiveButton(android.R.string.ok,
									new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dialog, int which) {
										}
									})
							.setIcon(android.R.drawable.ic_dialog_info)
							.show();


					buttonView.setChecked(false);
				} else {
					if(isChecked){
						if(!MainActivity.getInstance().getHoneyService().isRunning(item.protocol)) {
							MainActivity.getInstance().getHoneyService().startListener(item.protocol);
							mServicesSwitch.setOnCheckedChangeListener(null);
							mServicesSwitch.setChecked(true);
							mServicesSwitch.setOnCheckedChangeListener(mListener);
						}
						buttonView.setChecked(true);
					} else {
						if(MainActivity.getInstance().getHoneyService().isRunning(item.protocol)){
							MainActivity.getInstance().getHoneyService().stopListener(item.protocol);
						}
						buttonView.setChecked(false);
					}
				}
						}
					});
		return rowView;
	}

	private void updateStatus(ServicesListItem item, ViewHolder holder){
		if(MainActivity.getInstance().getHoneyService().isRunning(item.protocol)){
			holder.activated.setChecked(true);
			if(!MainActivity.getInstance().hasProtocolAttacks(item.protocol)){
				if(Integer.valueOf(item.attacks) > 0) {
					setBackground(holder, R.drawable.services_circle_yellow);
				}
				else {
					setBackground(holder, R.drawable.services_circle_green);
				}

			}
			else {
				if(MainActivity.getInstance().hasProtocolAttacks(item.protocol)){
					setBackground(holder, R.drawable.services_circle_red);
				}
			}
		}
		else {
			holder.activated.setChecked(false);
			setBackground(holder, R.drawable.services_circle);
		}
		holder.recordedAttacks.setText(String.format(MainActivity.getContext().getResources().getString(R.string.recorded_attacks) + "  %d", Integer.valueOf(item.attacks)));
	}

	private void setBackground(ViewHolder holder, int drawable){
		if(sdk < Build.VERSION_CODES.JELLY_BEAN){
			holder.circle.setBackgroundDrawable(MainActivity.getInstance().getResources().getDrawable(drawable));
		}
		else {
			holder.circle.setBackground(MainActivity.getInstance().getResources()
					.getDrawable(drawable));
		}
	}
}
