/*
 * Copyright (C) 2015 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.android.benchmark.ui;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.android.benchmark.registry.BenchmarkRegistry;
import com.android.benchmark.ui.automation.Automator;
import com.android.benchmark.ui.automation.Interaction;

import java.io.File;

public class TextScrollActivity extends ListActivityBase {

    public static final String EXTRA_HIT_RATE = ".TextScrollActivity.EXTRA_HIT_RATE";

    private static final int PARAGRAPH_COUNT = 200;

    private int mHitPercentage = 100;
    private Automator mAutomator;
    private String mName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        mHitPercentage = getIntent().getIntExtra(EXTRA_HIT_RATE,
                mHitPercentage);
        super.onCreate(savedInstanceState);
        final int runId = getIntent().getIntExtra("com.android.benchmark.RUN_ID", 0);
        final int iteration = getIntent().getIntExtra("com.android.benchmark.ITERATION", -1);
        final int id = getIntent().getIntExtra(BenchmarkRegistry.EXTRA_ID, -1);

        if (id == -1) {
            finish();
            return;
        }

        mName = BenchmarkRegistry.getBenchmarkName(this, id);

        mAutomator = new Automator(getName(), runId, iteration, getWindow(),
                new Automator.AutomateCallback() {
            @Override
            public void onPostAutomate() {
                Intent result = new Intent();
                setResult(RESULT_OK, result);
                finish();
            }

            @Override
            public void onAutomate() {
                ListView v = (ListView) findViewById(android.R.id.list);

                int[] coordinates = new int[2];
                v.getLocationOnScreen(coordinates);

                int x = coordinates[0];
                int y = coordinates[1];

                float width = v.getWidth();
                float height = v.getHeight();

                float middleX = (x + width) / 2;
                float middleY = (y + height) / 2;

                Interaction flingUp = Interaction.newFlingUp(middleX, middleY);
                Interaction flingDown = Interaction.newFlingDown(middleX, middleY);

                addInteraction(flingUp);
                addInteraction(flingDown);
                addInteraction(flingUp);
                addInteraction(flingDown);
                addInteraction(flingUp);
                addInteraction(flingDown);
                addInteraction(flingUp);
                addInteraction(flingDown);
            }
        });

        mAutomator.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mAutomator != null) {
            mAutomator.cancel();
            mAutomator = null;
        }
    }

    @Override
    protected ListAdapter createListAdapter() {
        return new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
                Utils.buildParagraphListWithHitPercentage(PARAGRAPH_COUNT, 80));
    }

    @Override
    protected String getName() {
        return mName;
    }
}
