package com.ericsson.uecontrol.gui.util;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ericsson.uecontrol.R;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.core.util.StringUtil;

import java.util.List;

/**
 * Created by ezivkoc on 2014-07-15.
 */
public class BehaviourListAdapter extends StableArrayAdapter<UeBehaviour>{
    private static final int VIEW_RESOURCE = R.layout.behaviour_list_item;

    private static LayoutInflater inflater = null;

    private boolean editable = false;

    public BehaviourListAdapter(Activity a, List<UeBehaviour> objects) {
        super(a, VIEW_RESOURCE, objects);
        inflater = (LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if(convertView==null)
            vi = inflater.inflate(VIEW_RESOURCE, null);

        ImageView draggable = (ImageView)vi.findViewById(R.id.draggable);
        TextView title = (TextView)vi.findViewById(R.id.title);
        TextView description = (TextView)vi.findViewById(R.id.description);
        final ImageView warning = (ImageView) vi.findViewById(R.id.warning);
        final ProgressBar active = (ProgressBar)vi.findViewById(R.id.active);
        final ProgressBar progress = (ProgressBar) vi.findViewById(R.id.progress);
        final TextView time = (TextView) vi.findViewById(R.id.time);

        UeBehaviour behaviour = super.getItem(position);

        // Setting all values in listview
        title.setText(behaviour.getName());
        description.setText(""+behaviour);
        time.setText(StringUtil.getTimeString(behaviour.getExecTime()));

        if(!behaviour.isRunning())
            time.setVisibility(View.VISIBLE);
        if(editable)
            draggable.setVisibility(View.VISIBLE);

        behaviour.setExecutionListener(new UeBehaviour.BehaviourExecutionListener() {
            public void executionStarted() {
                progress.post(new Runnable() {
                    public void run() {
                        time.setVisibility(View.INVISIBLE);
                        active.setVisibility(View.VISIBLE);
                        progress.setVisibility(View.VISIBLE);
                        warning.setVisibility(View.INVISIBLE);
                        progress.setMax(100);
                    }
                });
            }
            public void progressChanged(final float p) {
                if(active.getVisibility() != View.VISIBLE)
                    executionStarted();
                progress.setProgress((int) (p * 100));
            }
            public void executionStopped(final long execTime) {
                progress.post(new Runnable() {
                    public void run() {
                        active.setVisibility(View.INVISIBLE);
                        progress.setVisibility(View.INVISIBLE);
                        time.setText(StringUtil.getTimeString(execTime));
                        time.setVisibility(View.VISIBLE);
                    }
                });
            }
            public void exception(Exception e){
                warning.post(new Runnable() {
                    public void run() {
                        warning.setVisibility(View.VISIBLE);
                    }
                });
            }
        });

        return vi;
    }

    public void setEditable(boolean b) {
        editable = b;
    }

}
