package com.tianma.propertyanimationsample;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

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

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private FuncItemAdapter adapter;

    private List<FuncItem> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initViews();
    }

    private void initData() {
        data = new ArrayList<>();
        data.add(new FuncItem(getString(R.string.value_animator_sample), ValueAnimatorSampleActivity.class));
        data.add(new FuncItem(getString(R.string.object_animator_sample), ObjectAnimatorSampleActivity.class));
        data.add(new FuncItem(getString(R.string.xml_animator_sample), AnimatorXMLSampleActivity.class));
        data.add(new FuncItem(getString(R.string.advanced_animator_sample), AdvancedAnimatorSample.class));
        data.add(new FuncItem(getString(R.string.view_property_animator_sample), ViewPropertyAnimatorSample.class));
        data.add(new FuncItem(getString(R.string.evaluator_interpolator_contrast), TypeEvaluatorAndInterpolatorContrastSample.class));
    }

    private void initViews() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new FuncItemAdapter(this, data);
        recyclerView.setAdapter(adapter);
    }

    private class FuncItem {
        public String description;
        public Class<? extends Activity> target;

        public FuncItem(String description, Class<? extends Activity> target) {
            this.description = description;
            this.target = target;
        }
    }

    private class FuncItemAdapter extends RecyclerView.Adapter<FuncItemAdapter.FuncItemHolder> {

        private List<FuncItem> items;
        private Context context;

        public FuncItemAdapter(Context context, List<FuncItem> items) {
            this.items = items;
            this.context = context;
        }

        @Override
        public FuncItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_clicked, parent, false);
            return new FuncItemHolder(view);
        }

        @Override
        public void onBindViewHolder(FuncItemHolder holder, int position) {
            final FuncItem item = items.get(position);
            holder.button.setText(item.description);
            holder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, item.target);
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return items == null ? 0 : items.size();
        }

        class FuncItemHolder extends RecyclerView.ViewHolder {

            private Button button;

            public FuncItemHolder(View itemView) {
                super(itemView);
                button = (Button) itemView.findViewById(R.id.item_button);
            }

        }
    }
}
