package com.angry_glass_studios.notation.views;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import javax.inject.Inject;

import com.angry_glass_studios.notation.R;
import com.angry_glass_studios.notation.activity.NotelistActivity;
import com.angry_glass_studios.notation.events.Event;
import com.angry_glass_studios.notation.events.EventListener;
import com.angry_glass_studios.notation.models.NoteModel;

public class NotelistView extends LinearLayout {

  /**
   * The interface to send events from the view to the controller
   */
  public static interface ViewListener {
    // Custom view events are declared here. Add anything you wish to handle
    // (handling occurs in the Controllers; AKA the corresponding activity)
    public void onLaunchNote();
  }

  private static boolean DEBUG = false;
  private static final String TAG = NotelistView.class.getSimpleName();

  @InjectView(R.id.notelistFlEmpty) FrameLayout mNotelistFlEmpty;
  @InjectView(R.id.notelistIbtnNote) ImageButton mNotelistIbtnNote;
  @InjectView(R.id.notelistIbtnEdit) ImageButton mNotelistIbtnEdit;
  @InjectView(R.id.notelistIbtnSettings) ImageButton mNotelistIbtnSettings;
  private NoteModel model;


  /**
   * The listener reference for sending events
   */
  private ViewListener viewListener;
  public void setViewListener(ViewListener viewListener) {
    this.viewListener = viewListener;
  }

  /**
   * Constructor for xml layouts
   */
  public NotelistView(Context context, AttributeSet attrs) {
    super(context, attrs);
    model = ((NotelistActivity) context).getModel();
    ((Activity) context).requestWindowFeature(Window.FEATURE_NO_TITLE);
  }

  // If the controller needs to access View data based on UI state, you can
  // expose the methods here; typically to set a View state from controller

  /**
   * Remove the listener from the model
   */
  public void destroy() {
    // model.removeListener(NoteModel.ChangeEvent.LOCK_CHANGED, lockListener);
  }

  /**
   * Does the work to update the view when the model changes.
   */
  private void bind() {
    Log.i(TAG, "model updated, invoking bind()");

  }

  /**
   * Find our references to the objects in the xml layout
   */
  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();
    ButterKnife.inject(this);
    Log.i(TAG, "inflation complete!");

    // Add any necessary listeners to the model
    // Be sure to define anonymous inner classes if you want to be able
    // to see all the View event overrides at a glance
    // Wire the view events to the corresponding viewListener events
    // defined in the controller (this is how we communicate view events
    // to the controller)

    mNotelistIbtnNote.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Log.i(TAG, "notelist lbutton clicked");
        viewListener.onLaunchNote();
      }
    });

    mNotelistIbtnEdit.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Log.i(TAG, "notelist mbutton clicked");
      }
    });

    mNotelistIbtnSettings.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Log.i(TAG, "notelist rbutton clicked");
      }
    });

    // Register any EventListener with the model so that it can be
    // executed when the model changes.
    //model.addListener(NoteModel.ChangeEvent.LOCK_CHANGED, lockListener);
    // Update views based on updated model

    bind();
  }

  /**
   * The listener for when locked property changes on the model
   */
  /*
  private EventListener lockListener = new EventListener() {
    @Override
    public void onEvent(Event event) {
      bind();
    }
  };
  */

  public NoteModel getModel() {
    return model;
  }
}
