content: /*
   * Copyright (C) 2014 Andrew Comminos
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  package com.morlunk.mumbleclient.channel;
  
  import android.animation.Animator;
  import android.content.SharedPreferences;
  import android.content.res.TypedArray;
  import android.os.Bundle;
  import android.preference.PreferenceManager;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.PagerTabStrip;
  import android.support.v4.view.ViewPager;
  import android.util.TypedValue;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  import com.morlunk.jumble.IJumbleService;
  import com.morlunk.jumble.IJumbleSession;
  import com.morlunk.jumble.JumbleService;
  import com.morlunk.jumble.model.IUser;
  import com.morlunk.jumble.model.WhisperTarget;
  import com.morlunk.jumble.util.IJumbleObserver;
  import com.morlunk.jumble.util.JumbleObserver;
  import com.morlunk.jumble.util.VoiceTargetMode;
  import com.morlunk.mumbleclient.R;
  import com.morlunk.mumbleclient.Settings;
  import com.morlunk.mumbleclient.util.JumbleServiceFragment;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * Class to encapsulate both a ChannelListFragment and ChannelChatFragment.
   * Created by andrew on 02/08/13.
   */
  public class ChannelFragment extends JumbleServiceFragment implements SharedPreferences.OnSharedPreferenceChangeListener, ChatTargetProvider {
  
      private ViewPager mViewPager;
      private PagerTabStrip mTabStrip;
      private Button mTalkButton;
      private View mTalkView;
  
      private View mTargetPanel;
      private ImageView mTargetPanelCancel;
      private TextView mTargetPanelText;
  
      private ChatTarget mChatTarget;
      /** Chat target listeners, notified when the chat target is changed. */
      private List<OnChatTargetSelectedListener> mChatTargetListeners = new ArrayList<OnChatTargetSelectedListener>();
  
      /** True iff the talk button has been hidden (e.g. when muted) */
      private boolean mTalkButtonHidden;
  
      private JumbleObserver mObserver = new JumbleObserver() {
          @Override
          public void onUserTalkStateUpdated(IUser user) {
              if (getService().isConnected()) {
                  IJumbleSession session = getService().getSession();
                  if (user != null && user.getSession() == session.getSessionId()) {
                      // Manually set button selection colour when we receive a talk state update.
                      // This allows representation of talk state when using hot corners and PTT toggle.
                      switch (user.getTalkState()) {
                          case TALKING:
                          case SHOUTING:
                          case WHISPERING:
                              mTalkButton.setPressed(true);
                              break;
                          case PASSIVE:
                              mTalkButton.setPressed(false);
                              break;
                      }
                  }
              }
          }
  
          @Override
          public void onUserStateUpdated(IUser user) {
              if (getService().isConnected()) {
                  IJumbleSession session = getService().getSession();
                  if (user != null && user.getSession() == session.getSessionId()) {
                      configureInput();
                  }
              }
          }
  
          @Override
          public void onVoiceTargetChanged(VoiceTargetMode mode) {
              configureTargetPanel();
          }
      };
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setHasOptionsMenu(true);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_channel, container, false);
          mViewPager = (ViewPager) view.findViewById(R.id.channel_view_pager);
          mTabStrip = (PagerTabStrip) view.findViewById(R.id.channel_tab_strip);
          if(mTabStrip != null) {
              int[] attrs = new int[] { R.attr.colorPrimary, android.R.attr.textColorPrimaryInverse };
              TypedArray a = getActivity().obtainStyledAttributes(attrs);
              int titleStripBackground = a.getColor(0, -1);
              int titleStripColor = a.getColor(1, -1);
              a.recycle();
  
              mTabStrip.setTextColor(titleStripColor);
              mTabStrip.setTabIndicatorColor(titleStripColor);
              mTabStrip.setBackgroundColor(titleStripBackground);
              mTabStrip.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
          }
  
          mTalkView = view.findViewById(R.id.pushtotalk_view);
          mTalkButton = (Button) view.findViewById(R.id.pushtotalk);
          mTalkButton.setOnTouchListener(new View.OnTouchListener() {
  
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  switch (event.getAction()) {
                      case MotionEvent.ACTION_DOWN:
                          getService().onTalkKeyDown();
                          break;
                      case MotionEvent.ACTION_UP:
                          getService().onTalkKeyUp();
                          break;
                  }
                  return true;
              }
          });
          mTargetPanel = view.findViewById(R.id.target_panel);
          mTargetPanelCancel = (ImageView) view.findViewById(R.id.target_panel_cancel);
          mTargetPanelCancel.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (getService() == null || !getService().isConnected())
                      return;
  
                  IJumbleSession session = getService().getSession();
                  if (session.getVoiceTargetMode() == VoiceTargetMode.WHISPER) {
                      byte target = session.getVoiceTargetId();
                      session.setVoiceTargetId((byte) 0);
                      session.unregisterWhisperTarget(target);
                  }
              }
          });
          mTargetPanelText = (TextView) view.findViewById(R.id.target_panel_warning);
          configureInput();
          return view;
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
          preferences.registerOnSharedPreferenceChangeListener(this);
  
          if(mViewPager != null) { // Phone
              ChannelFragmentPagerAdapter pagerAdapter = new ChannelFragmentPagerAdapter(getChildFragmentManager());
              mViewPager.setAdapter(pagerAdapter);
          } else { // Tablet
              ChannelListFragment listFragment = new ChannelListFragment();
              Bundle listArgs = new Bundle();
              listArgs.putBoolean("pinned", isShowingPinnedChannels());
              listFragment.setArguments(listArgs);
              ChannelChatFragment chatFragment = new ChannelChatFragment();
  
              getChildFragmentManager().beginTransaction()
                      .replace(R.id.list_fragment, listFragment)
                      .replace(R.id.chat_fragment, chatFragment)
                      .commit();
          }
      }
  
      @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          super.onCreateOptionsMenu(menu, inflater);
          inflater.inflate(R.menu.channel_menu, menu);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          Settings settings = Settings.getInstance(getActivity());
          switch (item.getItemId()) {
              case R.id.menu_input_voice:
                  settings.setInputMethod(Settings.ARRAY_INPUT_METHOD_VOICE);
                  return true;
              case R.id.menu_input_ptt:
                  settings.setInputMethod(Settings.ARRAY_INPUT_METHOD_PTT);
                  return true;
              case R.id.menu_input_continuous:
                  settings.setInputMethod(Settings.ARRAY_INPUT_METHOD_CONTINUOUS);
                  return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      public void onPause() {
          super.onPause();
          if (getService() != null && getService().isConnected() &&
              !Settings.getInstance(getActivity()).isPushToTalkToggle()) {
              // XXX: This ensures that push to talk is disabled when we pause.
              // We don't want to leave the talk state active if the fragment is paused while pressed.
              getService().getSession().setTalkingState(false);
          }
      }
  
      @Override
      public void onDestroy() {
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
          preferences.unregisterOnSharedPreferenceChangeListener(this);
          super.onDestroy();
      }
  
      @Override
      public IJumbleObserver getServiceObserver() {
          return mObserver;
      }
  
      @Override
      public void onServiceBound(IJumbleService service) {
          super.onServiceBound(service);
          if (service.getConnectionState() == JumbleService.ConnectionState.CONNECTED) {
              configureTargetPanel();
              configureInput();
          }
      }
  
      private void configureTargetPanel() {
          if (!getService().isConnected())
              return;
  
          IJumbleSession session = getService().getSession();
          VoiceTargetMode mode = session.getVoiceTargetMode();
          if (mode == VoiceTargetMode.WHISPER) {
              WhisperTarget target = session.getWhisperTarget();
              mTargetPanel.setVisibility(View.VISIBLE);
              mTargetPanelText.setText(getString(R.string.shout_target, target.getName()));
          } else {
              mTargetPanel.setVisibility(View.GONE);
          }
      }
  
      /**
       * @return true if the channel fragment is set to display only the user's pinned channels.
       */
      private boolean isShowingPinnedChannels() {
          return getArguments() != null &&
                 getArguments().getBoolean("pinned");
      }
  
      /**
       * Configures the fragment in accordance with the user's interface preferences.
       */
      private void configureInput() {
          Settings settings = Settings.getInstance(getActivity());
  
          ViewGroup.LayoutParams params = mTalkView.getLayoutParams();
          params.height = settings.getPTTButtonHeight();
          mTalkButton.setLayoutParams(params);
  
          boolean muted = false;
          if (getService() != null && getService().isConnected()) {
              IUser user = getService().getSession().getSessionUser();
              muted = user.isMuted() || user.isSuppressed() || user.isSelfMuted();
          }
          boolean showPttButton =
                  !muted &&
                  settings.isPushToTalkButtonShown() &&
                  settings.getInputMethod().equals(Settings.ARRAY_INPUT_METHOD_PTT);
          setTalkButtonHidden(!showPttButton);
      }
  
      private void setTalkButtonHidden(final boolean hidden) {
          if (hidden ^ mTalkButtonHidden) {
              Settings settings = Settings.getInstance(getActivity());
              mTalkView.animate()
                      .setDuration(300)
                      .translationY(hidden ? settings.getPTTButtonHeight() : 0)
                      .setListener(new Animator.AnimatorListener() {
                          @Override
                          public void onAnimationStart(Animator animation) {
                              mTalkView.setVisibility(View.VISIBLE);
                          }
  
                          @Override
                          public void onAnimationEnd(Animator animation) {
                              mTalkView.setVisibility(hidden ? View.GONE : View.VISIBLE);
                          }
  
                          @Override
                          public void onAnimationCancel(Animator animation) {
  
                          }
  
                          @Override
                          public void onAnimationRepeat(Animator animation) {
  
                          }
                      });
          }
          mTalkButtonHidden = hidden;
      }
  
      @Override
      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
          if(Settings.PREF_INPUT_METHOD.equals(key)
              || Settings.PREF_PUSH_BUTTON_HIDE_KEY.equals(key)
              || Settings.PREF_PTT_BUTTON_HEIGHT.equals(key))
              configureInput();
      }
  
      @Override
      public ChatTarget getChatTarget() {
          return mChatTarget;
      }
  
      @Override
      public void setChatTarget(ChatTarget target) {
          mChatTarget = target;
          for(OnChatTargetSelectedListener listener : mChatTargetListeners)
              listener.onChatTargetSelected(target);
      }
  
      @Override
      public void registerChatTargetListener(OnChatTargetSelectedListener listener) {
          mChatTargetListeners.add(listener);
      }
  
      @Override
      public void unregisterChatTargetListener(OnChatTargetSelectedListener listener) {
          mChatTargetListeners.remove(listener);
      }
  
      private class ChannelFragmentPagerAdapter extends FragmentPagerAdapter {
  
          public ChannelFragmentPagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int i) {
              Fragment fragment = null;
              Bundle args = new Bundle();
              switch (i) {
                  case 0:
                      fragment = new ChannelListFragment();
                      args.putBoolean("pinned", isShowingPinnedChannels());
                      break;
                  case 1:
                      fragment = new ChannelChatFragment();
                      break;
              }
              fragment.setArguments(args);
              return fragment;
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              switch (position) {
                  case 0:
                      return getString(R.string.channel).toUpperCase();
                  case 1:
                      return getString(R.string.chat).toUpperCase();
                  default:
                      return null;
              }
          }
  
          @Override
          public int getCount() {
              return 2;
          }
      }
  }
  