package com.usai.redant.raimage.PhotoList;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.usai.redant.raimage.PhotoList.GridLayoutRecycler.RAPreviewRecyclerView;
import com.usai.redant.raimage.PhotoList.XuanImageView.XuanImageView;
import com.usai.redant.raimage.PhotoList.XuanImageView.XuanImageViewSettings;
import com.usai.redant.raimage.R;

import java.io.File;
import java.util.ArrayList;

public class NewPhotoPreviewActivity extends AppCompatActivity {

    private TextView indexTV;
//    private RAPreviewRecyclerView recyclerView;

    private ViewPager pager;
    private ArrayList<String> photos;
    private ArrayList<XuanImageView> ivContainer;
    private int currentIdx;
    private PreviewAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_photo_preview);

        /**Action Bar*/
        ActionBar mActionBar = getSupportActionBar();
        mActionBar.setHomeButtonEnabled(true);
        mActionBar.setDisplayHomeAsUpEnabled(true);
        mActionBar.setTitle("RA Image");


        ivContainer = new ArrayList<XuanImageView>();
        /**View*/
        indexTV = (TextView)findViewById(R.id.index_tv);
//        recyclerView = (RAPreviewRecyclerView) findViewById(R.id.preview_recycler);


        /** Setting */
        Intent intent = getIntent();
        photos = (ArrayList<String>) intent.getSerializableExtra("photos");
        currentIdx = intent.getIntExtra("index",0);

        if (photos.size() > 0) {
            indexTV.setText(currentIdx + 1 + " / " + photos.size());
        }

//        LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
//        recyclerView.setLayoutManager(layoutManager);
//        // 设置Item增加、移除动画
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
//        // 设置Adapter
//        recyclerView.setAdapter(adapter = new PreviewAdapter());
//
//        recyclerView.indicator = new RAPreviewRecyclerView.PageIndicator() {
//            @Override
//            public void changePageIndex(int index, int total) {
//                indexTV.setText(index + 1 + " / " + total);
//            }
//        };
//
//        recyclerView.scrollToPage(currentIdx);


        pager = (ViewPager)findViewById(R.id.preview_recycler);
        pager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return photos.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {

                XuanImageView xuanImageView = new XuanImageView(getBaseContext());
                xuanImageView.setAutoRotateCategory(XuanImageViewSettings.AUTO_ROTATE_CATEGORY_MAGNETISM);

                String path = photos.get(position);
                Bitmap bitmap = scaleImage(path,150,150);
                xuanImageView.setImageBitmap(bitmap);

//                Bitmap bitmap = BitmapFactory.decodeFile(path);
//                xuanImageView.setImageBitmap(bitmap);

                container.addView(xuanImageView);

                ivContainer.add(xuanImageView);

                return  xuanImageView;

            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                ivContainer.remove(position);
            }
        });

        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indexTV.setText(position + 1 + " / " + photos.size());
                currentIdx = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        pager.setCurrentItem(currentIdx);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home: {
                finish();
            }
            break;
        }
        return true;
    }

    /**Adapter*/
    public class PreviewAdapter extends RecyclerView.Adapter<PreviewAdapter.PreviewHolder> {

        class PreviewHolder extends RecyclerView.ViewHolder {
            XuanImageView imageView;
            public PreviewHolder(View view) {
                super(view);
                imageView = (XuanImageView)view.findViewById(R.id.preview_item);
            }
        }

        @Override
        public PreviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            PreviewHolder holder = new PreviewHolder(LayoutInflater.from(NewPhotoPreviewActivity.this).inflate(R.layout.photo_preview_item,parent,false));
            return holder;
        }

        @Override
        public void onBindViewHolder(PreviewHolder holder, int position) {
            String path = photos.get(position);
//            File file = new File(path);
//            Uri uri = Uri.fromFile(file);
//            holder.imageView.setImageURI(uri);

            Bitmap bitmap = scaleImage(path,150,150);

            holder.imageView.setImageBitmap(bitmap);
        }

        @Override
        public int getItemCount() {
            return photos.size();
        }
    }


    /** View Pager */

    public Bitmap scaleImage(String path, int width, int height) {
        Bitmap source = BitmapFactory.decodeFile(path);

        Bitmap scaled = Bitmap.createScaledBitmap(source, source.getWidth() / 2, source.getHeight() / 2, true);
        return scaled;
    }

}
