/*
 * Copyright 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.sample.oboe.manualtest;

import android.media.audiofx.Equalizer;
import android.media.audiofx.PresetReverb;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;

import com.google.sample.oboe.manualtest.R;


abstract class TestOutputActivityBase extends TestAudioActivity {
    AudioOutputTester mAudioOutTester;

//    protected TextView mTextThreshold;
//    protected SeekBar mFaderThreshold;
//    protected ExponentialTaper mTaperThreshold;
    protected TextView mTextAmplitude;
    protected SeekBar mFaderAmplitude;
    protected ExponentialTaper mTaperAmplitude;
    private BufferSizeView mBufferSizeView;

    @Override boolean isOutput() { return true; }

    protected void updateEnabledWidgets() {
        super.updateEnabledWidgets();
//        for (StreamContext streamContext : mStreamContexts) {
//            boolean thresholdSupported = (streamContext.tester == null) ? false :
//                    streamContext.tester.getCurrentAudioStream().isThresholdSupported();
//            mFaderThreshold.setEnabled(getState() == STATE_STARTED && thresholdSupported);
//        }
    }

    private SeekBar.OnSeekBarChangeListener mAmplitudeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            double amplitude = mTaperAmplitude.linearToExponential(progress);
            mAudioOutTester.setAmplitude(amplitude);
            mTextAmplitude.setText("Amplitude = " + amplitude);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    };
//
//    private SeekBar.OnSeekBarChangeListener mThresholdListener = new SeekBar.OnSeekBarChangeListener() {
//        @Override
//        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//            setBufferSizeByPosition(progress);
//        }
//
//        @Override
//        public void onStartTrackingTouch(SeekBar seekBar) {
//        }
//
//        @Override
//        public void onStopTrackingTouch(SeekBar seekBar) {
//        }
//    };
//
//    private void setBufferSizeByPosition(int progress) {
//        double normalizedThreshold = mTaperThreshold.linearToExponential(progress);
//        if (normalizedThreshold < 0.0) normalizedThreshold = 0.0;
//        else if (normalizedThreshold > 1.0) normalizedThreshold = 1.0;
//        if (mAudioOutTester != null) {
//            mAudioOutTester.setNormalizedThreshold(normalizedThreshold);
//            int percent = (int) (normalizedThreshold * 100);
//            int bufferSize = mAudioOutTester.getCurrentAudioStream().getBufferSizeInFrames();
//            int bufferCapacity = mAudioOutTester.getCurrentAudioStream().getBufferCapacityInFrames();
//            mTextThreshold.setText("bufferSize = " + percent + "% = "
//                    + bufferSize + " / " + bufferCapacity);
//        }
//    }

    protected void findAudioCommon() {
        super.findAudioCommon();

        mBufferSizeView = (BufferSizeView) findViewById(R.id.buffer_size_view);
        mTextAmplitude = (TextView) findViewById(R.id.textAmplitude);
        mFaderAmplitude = (SeekBar) findViewById(R.id.faderAmplitude);
        mFaderAmplitude.setOnSeekBarChangeListener(mAmplitudeListener);
        mTaperAmplitude = new ExponentialTaper(FADER_THRESHOLD_MAX, 0.0, 4.0, 100.0);
    }

    @Override
    public AudioOutputTester addAudioOutputTester() {
        AudioOutputTester audioOutTester = super.addAudioOutputTester();
        mBufferSizeView.setAudioOutTester(audioOutTester);
        return audioOutTester;
    }

    public void pauseAudio() {
        super.pauseAudio();
    }

    public void stopAudio() {
        super.stopAudio();
    }

    // TODO Add editor
    public void setupEqualizer(int sessionId) {
        Equalizer equalizer = new Equalizer(0, sessionId);
        int numBands = equalizer.getNumberOfBands();
        Log.d(TAG, "numBands " + numBands);
        for (short band = 0; band < numBands; band++) {
            String msg = "band " + band
                    + ", center = " + equalizer.getCenterFreq(band)
                    + ", level = " + equalizer.getBandLevel(band);
            Log.d(TAG, msg);
            equalizer.setBandLevel(band, (short)40);
        }

        equalizer.setBandLevel((short) 1, (short) 300);
    }

    public void setupReverb(int sessionId) {
        PresetReverb effect = new PresetReverb(0, sessionId);
    }

    @Override
    public void setupEffects(int sessionId) {
        // setupEqualizer(sessionId);
        // setupReverb(sessionId);
    }
}
@@ -982,140 +982,8 @@
 r;%0A%0A
-//    protected TextView mTextThreshold;%0A//    protected SeekBar mFaderThreshold;%0A//    protected ExponentialTaper mTaperThreshold;%0A
     
@@ -1204,441 +1204,8 @@
  %7D%0A%0A
-    protected void updateEnabledWidgets() %7B%0A        super.updateEnabledWidgets();%0A//        for (StreamContext streamContext : mStreamContexts) %7B%0A//            boolean thresholdSupported = (streamContext.tester == null) ? false :%0A//                    streamContext.tester.getCurrentAudioStream().isThresholdSupported();%0A//            mFaderThreshold.setEnabled(getState() == STATE_STARTED && thresholdSupported);%0A//        %7D%0A    %7D%0A%0A
     
@@ -1798,1320 +1798,14 @@
   %7D%0A
+
     %7D;
-%0A//%0A//    private SeekBar.OnSeekBarChangeListener mThresholdListener = new SeekBar.OnSeekBarChangeListener() %7B%0A//        @Override%0A//        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) %7B%0A//            setBufferSizeByPosition(progress);%0A//        %7D%0A//%0A//        @Override%0A//        public void onStartTrackingTouch(SeekBar seekBar) %7B%0A//        %7D%0A//%0A//        @Override%0A//        public void onStopTrackingTouch(SeekBar seekBar) %7B%0A//        %7D%0A//    %7D;%0A//%0A//    private void setBufferSizeByPosition(int progress) %7B%0A//        double normalizedThreshold = mTaperThreshold.linearToExponential(progress);%0A//        if (normalizedThreshold %3C 0.0) normalizedThreshold = 0.0;%0A//        else if (normalizedThreshold %3E 1.0) normalizedThreshold = 1.0;%0A//        if (mAudioOutTester != null) %7B%0A//            mAudioOutTester.setNormalizedThreshold(normalizedThreshold);%0A//            int percent = (int) (normalizedThreshold * 100);%0A//            int bufferSize = mAudioOutTester.getCurrentAudioStream().getBufferSizeInFrames();%0A//            int bufferCapacity = mAudioOutTester.getCurrentAudioStream().getBufferCapacityInFrames();%0A//            mTextThreshold.setText(%22bufferSize = %22 + percent + %22%25 = %22%0A//                    + bufferSize + %22 / %22 + bufferCapacity);%0A//        %7D%0A//    %7D
 %0A%0A  

remove some dead code