private void shareEvent() {
    // Get the current event
    Event event = imin.getUser().getCurrentEvent();

    // Get the path
    Imin.shareEvent(this, event);
  }
  @Override
  public void onEventClosed() {
    Event event = imin.getUser().getCurrentEvent();

    // Set closed flag
    event.setClosed(true);
    event.setFinalDateTimeProposal(imin.getUser().getSelectedDateTimeProposal());
    event.setFinalLocationProposal(imin.getUser().getSelectedLocationProposal());

    // Event closed
    dialogCloseProgress.dismiss();

    // Show information dialog
    dialogClosed();
  }
  private void finishVoting() {
    if (closing) {
      finishClosing();

      return;
    }

    if (!modified) {
      // Come back to the previous activity since the event is not modified
      dialogResponded();

      return;
    }

    // Get the current selected event
    Event event = imin.getUser().getCurrentEvent();

    // Get the event proposals
    List<Proposal> proposals = event.getProposals();

    // Analyze the responses
    List<Response> responses = new ArrayList<Response>();

    // Analyze the poll table
    for (Proposal proposal : proposals) {
      if (proposal.getProposalType() == Proposal.PROPOSAL_TYPE_LOCATION) {
        // Look up the table and get the response of the user for that
        Response response = proposal.getContactResponse(imin.getUser().getPublicUserId());

        if (response != null) {
          // Add to the list of responses
          responses.add(response);
        }
      }
    }

    showRespondDialog();

    // Add the datetime responses
    responses.addAll(imin.dateTimeResponses);

    // At this point, we have the list of responses
    configurationInstance.taskRespondEvent = new TaskRespondEvent(this);
    TaskRespondEventParams params =
        new TaskRespondEventParams(imin.getUser().getPrivateUserId(), event, responses);
    configurationInstance.taskRespondEvent.execute(params);
  }
  @Override
  public void onEventResponded() {
    Event event = imin.getUser().getCurrentEvent();

    // Flag the event as responded
    event.setResponded(true);

    imin.getUser().setPollResponded(true);

    // Event closed
    dialogRespondProgress.dismiss();

    // Event responded
    dialogResponded();

    // Analytics event
    Analytics.send(this, Analytics.ANALYTICS_EVENT_VOTE);
  }
  private void initializeObjects() {
    TextView textEventName;
    Button buttonYourVote;
    Button btnFinish;

    // Save object references
    layoutSelectLocations = (LinearLayout) findViewById(R.id.layoutSelectLocations);
    layoutCloseEvent = (LinearLayout) findViewById(R.id.layoutCloseEvent);
    textEventName = (TextView) findViewById(R.id.textEventName);
    btnFinish = (Button) findViewById(R.id.btnFinish);
    buttonYourVote = (Button) findViewById(R.id.buttonYourVote);
    layoutPollLocations = (LinearLayout) findViewById(R.id.layoutPollLocations);
    layoutPollUserVotes = (LinearLayout) findViewById(R.id.layoutPollUserVotes);
    layoutPollCounters = (LinearLayout) findViewById(R.id.layoutPollCounters);
    layoutPollPoll = (LinearLayout) findViewById(R.id.layoutPollPoll);

    // Restore dialogs
    restoreDialogs();

    // Set listeners for handling events
    btnFinish.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            finishVoting();
          }
        });

    // Restore the 'modified' value
    modified = getIntent().getBooleanExtra("modified", modified);

    // Get the current selected event
    Event event = imin.getUser().getCurrentEvent();

    // Put the event name
    String eventName = event.getName();
    textEventName.setText(eventName);

    if (closing) {
      buttonYourVote.setText("");
    }

    /*
          // Check if the event has been created by the user
    if (event.isAdmin()) {
    	// Check if it is closed
    	if (event.isClosed()) {
    		// Event closed
    	} else {
    		// btnCloseEvent.setVisibility(View.VISIBLE);
    	}
    }
    */
  }
  private void finishClosing() {
    if (!proposalSelected) {
      DialogInformation.build(this)
          .show(getString(R.string.tienes_que_seleccionar_un_lugar), DialogInformation.ICON_ALERT);
      return;
    }

    Event event = imin.getUser().getCurrentEvent();

    event.setFinalDateTimeProposal(imin.getUser().getSelectedDateTimeProposal());
    event.setFinalLocationProposal(imin.getUser().getSelectedLocationProposal());
    event.setFinalDateTimeProposalId(
        imin.getUser().getSelectedDateTimeProposal().getPublicProposalId());
    event.setFinalLocationProposalId(
        imin.getUser().getSelectedLocationProposal().getPublicProposalId());

    // Ask the user
    DialogProposal dialogProposal =
        DialogProposal.build(
            this,
            new DialogProposal.OnClickListener() {
              @Override
              public void onClick(Event event, boolean result) {
                if (result) {
                  // Yes button clicked
                  eventClose();
                }
              }
            });

    dialogProposal.show(imin.getUser(), imin.getUser().getCurrentEvent());
  }
  @Override
  public void onEventReopened() {
    Event event = imin.getUser().getCurrentEvent();

    // Set closed flag
    event.setClosed(false);

    // Event reopened
    dialogCloseProgress.dismiss();

    // Show a Toast notifying
    DialogInformation.build(this)
        .show(getString(R.string.event_reopened), DialogInformation.ICON_INFO);
  }
  // Get the current selected event
  private void eventClose() {
    Event event = imin.getUser().getCurrentEvent();

    // Go ahead with event closing
    showCloseDialog();

    Proposal finalDateTimeProposal = event.getFinalDateTimeProposal();
    Proposal finalLocationProposal = event.getFinalLocationProposal();
    String finalDateTimeProposalId = finalDateTimeProposal.getPublicProposalId();
    String finalLocationProposalId = finalLocationProposal.getPublicProposalId();
    event.setFinalDateTimeProposalId(finalDateTimeProposalId);
    event.setFinalLocationProposalId(finalDateTimeProposalId);

    // Run an asynchronous task to retrieve the single event
    configurationInstance.taskCloseEvent = new TaskCloseEvent(this);
    TaskCloseEventParams params =
        new TaskCloseEventParams(
            imin.getUser().getPrivateUserId(),
            event.getId(),
            true,
            finalDateTimeProposalId,
            finalLocationProposalId);
    configurationInstance.taskCloseEvent.execute(params);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_poll_locations);
    Imin.overrideFonts(findViewById(android.R.id.content));

    inflater = LayoutInflater.from(this);

    // Get the Imin application object
    imin = ((Imin) getApplication());
    context = this;

    Intent intent = getIntent();
    closing = intent.getBooleanExtra(Imin.EXTRA_EVENT_POLL_CLOSE, false);

    // Initialize objects
    initializeObjects();

    // Check if the the activity has been opened for closing the event
    checkClose();

    // Load values and fill
    fillValues();
  }
  private void paintCounters() {
    // Iterate through the counters
    for (int k = 0; k < layoutPollUserVotes.getChildCount(); ++k) {
      // Get the user response
      View layoutPollUserVote = layoutPollUserVotes.getChildAt(k);
      Button btnViewPollVote = (Button) layoutPollUserVote.findViewById(R.id.btnViewPollVote);
      Proposal proposal = (Proposal) btnViewPollVote.getTag();

      View layoutPollCounter = layoutPollCounters.getChildAt(k);
      Button btnPollCounter = (Button) layoutPollCounter.findViewById(R.id.btnPollCounter);

      // Get the response of ourselves
      Response response = proposal.getContactResponse(imin.getUser().getPublicUserId());

      // Get number of votes
      List<Contact> attendingContacts = proposal.getContacts(Response.RESPONSE_TYPE_ATTENDING);

      // Set the counter text
      int num_attending_contacts = attendingContacts.size();
      String counter = String.valueOf(num_attending_contacts);
      btnPollCounter.setText(counter);

      if (closing) {
        btnPollCounter.setEnabled(false);
        btnPollCounter.setTextColor(getResources().getColor(R.color.black));
      } else {
        if (response != null && response.getResponseType() == Response.RESPONSE_TYPE_ATTENDING) {
          btnPollCounter.setEnabled(true);
          btnPollCounter.setTextColor(getResources().getColor(R.color.white));
        } else {
          btnPollCounter.setEnabled(false);
          btnPollCounter.setTextColor(getResources().getColor(R.color.black));
        }
      }
    }
  }
 private void contactsDialog(Proposal proposal) {
   // Show the contacts in a custom dialog
   DialogContacts dialogContacts = DialogContacts.build(this);
   dialogContacts.show(imin.getUser(), proposal);
 }
  private void fillValues() {
    // Animator animator;

    // Clear the poll before recreating it
    layoutPollLocations.removeAllViews();
    layoutPollPoll.removeAllViews();
    layoutPollCounters.removeAllViews();
    layoutPollUserVotes.removeAllViews();

    // Get the current selected event
    Event event = imin.getUser().getCurrentEvent();

    if (!event.isResponded()) {
      modified = true;
    }

    // Get the list of proposals
    List<Proposal> proposals = event.getProposals();

    // =================================================== //
    // FILL LOCATION NAMES ON THE TOP

    // Retrieve the possible days of all the proposals
    List<Location> locations = event.getProposalLocations();

    // Sort the locations
    Collections.sort(locations);

    for (Location location : locations) {
      // For each day, create all the layouts
      String locationName = location.getName();

      // Find the corresponding proposal
      boolean found = false;
      Proposal proposal = null;
      for (Proposal proposalToFind : proposals) {
        if (proposalToFind.getLocation() == location) {
          // Found
          found = true;
          proposal = proposalToFind;
          break;
        }
      }

      if (found) {
        // Get the list of attending contacts for this proposal
        List<Contact> attendingContacts = proposal.getContacts(Response.RESPONSE_TYPE_ATTENDING);

        // ==================================================
        // HEADER ON THE TOP SHOWING DATE AND TIME

        // Inflate location layout
        View layoutPollLocation = inflater.inflate(R.layout.layout_poll_location, null);
        Button btnViewPollLocation =
            (Button) layoutPollLocation.findViewById(R.id.btnViewPollLocation);

        btnViewPollLocation.setText(locationName);

        btnViewPollLocation.setTag(proposal);
        btnViewPollLocation.setOnLongClickListener(
            new OnLongClickListener() {
              @Override
              public boolean onLongClick(View view) {
                Proposal proposal = (Proposal) view.getTag();
                contactsDialog(proposal);
                return true;
              }
            });

        // Finally, add the location to the list of locations
        layoutPollLocations.addView(layoutPollLocation);

        // Inflate votes linear layout
        LinearLayout layoutPollVotes =
            (LinearLayout) inflater.inflate(R.layout.layout_poll_votes, null);

        // ==================================================
        // USER RESPONSES

        int responseType;

        // Find user response
        String publicUserId = imin.getUser().getPublicUserId();
        Response response = proposal.getContactResponse(publicUserId);

        if (response != null) {
          // Responded, let's see which response has the user voted
          if (response.getResponseType() == Response.RESPONSE_TYPE_ATTENDING) {
            responseType = Response.RESPONSE_TYPE_ATTENDING;
          } else if (response.getResponseType() == Response.RESPONSE_TYPE_NOT_ATTENDING) {
            responseType = Response.RESPONSE_TYPE_NOT_ATTENDING;
          } else {
            responseType = Response.RESPONSE_TYPE_NOT_ATTENDING;
          }
        } else {
          // Not responded
          responseType = Response.RESPONSE_TYPE_NOT_ATTENDING;
        }

        // Inflate the option view
        View layoutPollUserVote = inflater.inflate(R.layout.layout_poll_location_user_vote, null);
        Button btnViewPollVote = (Button) layoutPollUserVote.findViewById(R.id.btnViewPollVote);

        if (closing) {
          btnViewPollVote.setBackgroundResource(
              R.drawable.button_poll_option_not_attending_background);
        } else {
          // Set the background of the view
          paintResponse(btnViewPollVote, responseType);
        }

        // Set the button clickable
        btnViewPollVote.setTag(proposal);
        btnViewPollVote.setOnClickListener(
            new OnClickListener() {
              @Override
              public void onClick(View view) {
                if (!closing) {
                  // Set the modified flag
                  modified = true;

                  // Get the proposal of the view
                  Proposal proposal = (Proposal) view.getTag();

                  // Get the response of ourselves
                  Response response = proposal.getContactResponse(imin.getUser().getPublicUserId());

                  if (response != null) {
                    // Increment the response type
                    response.incrementResponseType();
                  } else {
                    // Create
                    response =
                        new Response(
                            imin.getUser().getContact(),
                            Response.RESPONSE_TYPE_ATTENDING,
                            proposal);

                    // Finally, add the user response
                    proposal.addResponse(response);
                  }

                  int responseType = response.getResponseType();

                  // Paint the response
                  Button btnVote = (Button) view;
                  paintResponse(btnVote, responseType);

                  // Paint the counters
                  paintCounters();
                } else {
                  // Save the proposal of the view
                  imin.getUser().setSelectedLocationProposal((Proposal) view.getTag());

                  proposalSelected = true;

                  // Reset user votes and counters
                  paintUserVotes();
                  paintCounters();

                  view.setBackgroundResource(R.drawable.button_poll_option_attending_background);
                }
              }
            });

        // So, add the option
        layoutPollUserVotes.addView(layoutPollUserVote);

        // ==================================================
        // COUNTERS

        // Inflate the option view
        View layoutPollCounter = inflater.inflate(R.layout.layout_poll_location_counter, null);

        layoutPollCounters.addView(layoutPollCounter);

        // ==================================================
        // RESPONSES FROM THE REST OF CONTACTS

        for (Contact contact : attendingContacts) {
          // Inflate the option view
          View layoutPollVote = inflater.inflate(R.layout.layout_poll_location_vote, null);
          ResizableImageView imageViewPollContact =
              (ResizableImageView) layoutPollVote.findViewById(R.id.imageViewPollContact);
          TextView textViewPollContact =
              (TextView) layoutPollVote.findViewById(R.id.textViewPollContact);

          // Handle click
          imageViewPollContact.setTag(contact);
          imageViewPollContact.setOnClickListener(
              new OnClickListener() {
                @Override
                public void onClick(View view) {
                  Contact contact = (Contact) view.getTag();
                  Imin.showContactProfile(context, contact);
                }
              });

          textViewPollContact.setTag(contact);
          textViewPollContact.setOnClickListener(
              new OnClickListener() {
                @Override
                public void onClick(View view) {
                  Contact contact = (Contact) view.getTag();
                  Imin.showContactProfile(context, contact);
                }
              });

          // Set the contact photo
          Bitmap contactPhoto = contact.getPhoto();
          if (contactPhoto != null) {
            imageViewPollContact.setImageBitmap(contactPhoto);
            imageViewPollContact.setVisibility(View.VISIBLE);
            textViewPollContact.setVisibility(View.GONE);
          } else {
            // Capital letter in the box
            String contactName = contact.getName();
            if (contactName.length() > 0) {
              textViewPollContact.setText(contactName.substring(0, 1));
              textViewPollContact.setBackgroundColor(contact.getColor());
            }

            imageViewPollContact.setVisibility(View.GONE);
            textViewPollContact.setVisibility(View.VISIBLE);
          }

          // So, add the option
          layoutPollVotes.addView(layoutPollVote);
        }

        // Inflate the option view
        View layoutPollHiddenContact =
            inflater.inflate(R.layout.layout_poll_location_hidden_contact, null);

        // Add a hidden layout just for filling the list
        layoutPollVotes.addView(layoutPollHiddenContact);

        // Finally, add the list of contacts who are attending
        layoutPollPoll.addView(layoutPollVotes);
      }
    }

    paintCounters();

    // Set fonts
    Imin.overrideFonts(layoutPollLocations, Imin.fontRegular);
    Imin.overrideFonts(layoutPollCounters, Imin.fontRegular);
    Imin.overrideFonts(layoutPollPoll, Imin.fontRegular);
    Imin.overrideFonts(layoutPollUserVotes, Imin.fontRegular);
  }