package com.twilio.video.quickstart.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.view.MenuItem;

import com.twilio.video.AudioCodec;
import com.twilio.video.G722Codec;
import com.twilio.video.H264Codec;
import com.twilio.video.IsacCodec;
import com.twilio.video.OpusCodec;
import com.twilio.video.PcmaCodec;
import com.twilio.video.PcmuCodec;
import com.twilio.video.VideoCodec;
import com.twilio.video.Vp8Codec;
import com.twilio.video.Vp9Codec;
import com.twilio.video.quickstart.R;

import org.webrtc.MediaCodecVideoDecoder;
import org.webrtc.MediaCodecVideoEncoder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SettingsActivity extends AppCompatActivity {
    public static final String PREF_AUDIO_CODEC = "audio_codec";
    public static final String PREF_AUDIO_CODEC_DEFAULT = OpusCodec.NAME;
    public static final String PREF_VIDEO_CODEC = "video_codec";
    public static final String PREF_VIDEO_CODEC_DEFAULT = Vp8Codec.NAME;
    public static final String PREF_SENDER_MAX_AUDIO_BITRATE = "sender_max_audio_bitrate";
    public static final String PREF_SENDER_MAX_AUDIO_BITRATE_DEFAULT = "0";
    public static final String PREF_SENDER_MAX_VIDEO_BITRATE = "sender_max_video_bitrate";
    public static final String PREF_SENDER_MAX_VIDEO_BITRATE_DEFAULT = "0";

    private static final String[] VIDEO_CODEC_NAMES = new String[] {
            Vp8Codec.NAME, H264Codec.NAME, Vp9Codec.NAME
    };

    private static final String[] AUDIO_CODEC_NAMES = new String[] {
            IsacCodec.NAME, OpusCodec.NAME, PcmaCodec.NAME, PcmuCodec.NAME, G722Codec.NAME
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupActionBar();
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        SettingsFragment settingsFragment = SettingsFragment.newInstance(sharedPreferences);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(android.R.id.content, settingsFragment)
                .commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void setupActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            // Show the Up button in the action bar.
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        private SharedPreferences sharedPreferences;

        public static SettingsFragment newInstance(SharedPreferences sharedPreferences) {
            SettingsFragment settingsFragment = new SettingsFragment();
            settingsFragment.sharedPreferences = sharedPreferences;

            return settingsFragment;
        }

        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            addPreferencesFromResource(R.xml.settings);
            setHasOptionsMenu(true);
            setupCodecListPreference(AudioCodec.class,
                    PREF_AUDIO_CODEC,
                    PREF_AUDIO_CODEC_DEFAULT,
                    (ListPreference) findPreference(PREF_AUDIO_CODEC));
            setupCodecListPreference(VideoCodec.class,
                    PREF_VIDEO_CODEC,
                    PREF_VIDEO_CODEC_DEFAULT,
                    (ListPreference) findPreference(PREF_VIDEO_CODEC));
            setupSenderBandwidthPreferences(PREF_SENDER_MAX_AUDIO_BITRATE,
                    PREF_SENDER_MAX_AUDIO_BITRATE_DEFAULT,
                    (EditTextPreference) findPreference(PREF_SENDER_MAX_AUDIO_BITRATE));
            setupSenderBandwidthPreferences(PREF_SENDER_MAX_VIDEO_BITRATE,
                    PREF_SENDER_MAX_VIDEO_BITRATE_DEFAULT,
                    (EditTextPreference) findPreference(PREF_SENDER_MAX_VIDEO_BITRATE));
        }



        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == android.R.id.home) {
                startActivity(new Intent(getActivity(), SettingsActivity.class));
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        private void setupCodecListPreference(Class codecClass,
                                              String key,
                                              String defaultValue,
                                              ListPreference preference) {
            List<String> codecEntries = (codecClass == AudioCodec.class) ?
                    Arrays.asList(AUDIO_CODEC_NAMES) :
                    Arrays.asList(VIDEO_CODEC_NAMES);

            // Remove H264 if not supported
            if (!MediaCodecVideoDecoder.isH264HwSupported() ||
                    !MediaCodecVideoEncoder.isH264HwSupported()) {
                codecEntries.remove(H264Codec.NAME);
            }
            String[] codecStrings = (String[]) codecEntries.toArray();

            // Saved value
            final String value = sharedPreferences.getString(key, defaultValue);

            // Bind values
            preference.setEntries(codecStrings);
            preference.setEntryValues(codecStrings);
            preference.setValue(value);
            preference.setSummary(value);
            preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    preference.setSummary(newValue.toString());
                    return true;
                }
            });
        }

        private void setupSenderBandwidthPreferences(String key,
                                                     String defaultValue,
                                                     EditTextPreference editTextPreference) {
            String value = sharedPreferences.getString(key, defaultValue);

            // Set layout with input type number for edit text
            editTextPreference.setDialogLayoutResource(R.layout.preference_dialog_number_edittext);
            editTextPreference.setSummary(value);
            editTextPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    preference.setSummary(newValue.toString());
                    return true;
                }
            });
        }
    }
}

@@ -1046,21 +1046,26 @@
 va.util.
-Array
+Collection
 s;%0Aimpor
@@ -5243,16 +5243,49 @@
 tries = 
+new ArrayList%3C%3E();%0A            if
 (codecCl
@@ -5312,10 +5312,9 @@
 ass)
- ?
+%7B
 %0A   
@@ -5330,81 +5330,129 @@
     
-    Arrays.asList(AUDIO_CODEC_NAMES) :%0A                    Arrays.asList(
+Collections.addAll(codecEntries, AUDIO_CODEC_NAMES);%0A            %7Delse%7B%0A                Collections.addAll(codecEntries, 
 VIDE
@@ -5466,16 +5466,30 @@
 _NAMES);
+%0A            %7D
 %0A%0A      
@@ -5763,19 +5763,40 @@
 s = 
-(
+codecEntries.toArray(new 
 String%5B
-%5D) 
 code
@@ -5804,24 +5804,23 @@
 Entries.
-toArray(
+size()%5D
 );%0A%0A    

fix Arrays.asList to Collections.addAll (#294)