package com.kuckovsky.richard.home;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import io.socket.client.Ack;

/**
 * Created by richard on 5.6.17.
 */

public class roomOutputAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private JSONArray mDataSource;

    public roomOutputAdapter(Context context, JSONArray items) {
        mContext = context;
        mDataSource = items;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mDataSource.length();
    }

    @Override
    public Object getItem(int position) {
        try {
            return mDataSource.getJSONObject(position);
        } catch (JSONException e) {
            return 0;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = null;
        String type;

        try {
            final JSONObject output = mDataSource.getJSONObject(position);
            type = output.getString("type");
            Log.i("output", output.toString());
            if(type.compareTo("switch") == 0) {
                rowView = mInflater.inflate(R.layout.list_view_room_output_switch, parent, false);

                TextView title = (TextView) rowView.findViewById(R.id.output_name);
                final String name = output.getString("name");
                final Switch s = (Switch) rowView.findViewById(R.id.switch1);
                if(output.getInt("value") == output.getInt("min_value")) {
                    s.setChecked(false);
                } else {
                    s.setChecked(true);
                }

                s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
                        if (buttonView.isPressed()) {
                            output.remove("value");
                            try {
                                if (isChecked) {
                                    output.put("value", output.getInt("max_value"));
                                } else {
                                    output.put("value", output.getInt("min_value"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            JSONObject data = output;


                            SocketIO.getInstance().mSocket.emit("change_output", data, new Ack() {
                                @Override
                                public void call(Object... args) {

                                    JSONObject obj = (JSONObject) args[0];
                                    Log.i("load", obj.toString());

                                    try {
                                        if (obj.getInt("status") == 0) {
                                            output.remove("value");
                                            if (isChecked) {
                                                output.put("value", output.getInt("min_value"));

                                                new Handler(Looper.getMainLooper()).post(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        buttonView.setChecked(false);
                                                    }
                                                });

                                            } else {
                                                output.put("value", output.getInt("max_value"));

                                                new Handler(Looper.getMainLooper()).post(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        buttonView.setChecked(true);
                                                    }
                                                });
                                            }
                                        }

                                    } catch (JSONException e) {
                                        Log.e("Chyba", "");
                                        return;
                                    }

                                }
                            });
                        }
                    }
                });


                title.setText(output.getString("name"));

            } else if(type.compareTo("slider") == 0) {
                rowView = mInflater.inflate(R.layout.list_view_room_output_slider, parent, false);

                TextView title = (TextView) rowView.findViewById(R.id.output_name);
                SeekBar s = (SeekBar) rowView.findViewById(R.id.slider);
                s.setMax(0);
                s.setMax(output.getInt("max_value") - output.getInt("min_value"));
                s.setProgress(output.getInt("value") - output.getInt("min_value"));
                title.setText(output.getString("name"));
            } else {
                rowView = mInflater.inflate(R.layout.list_view_room_sensor, parent, false);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        //title.setTextColor(Color.BLACK);
        return rowView;
    }

}
