package com.usai.redant.raimage.PhotoList;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.net.Uri;
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.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.usai.redant.raimage.R;

import java.io.File;
import java.io.Serializable;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;

public class PhotoListActivity extends AppCompatActivity {

    private ArrayList<String> photos;
    private ArrayList<String> deletIndexs;
    private PhotoListAdapter adapter;
    private GridLayoutManager layoutManager;
    private Button deleteBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_list);

        Intent intent = getIntent();
        photos = (ArrayList<String>) intent.getSerializableExtra("pic_list");
        deletIndexs = new ArrayList<String>();
        for (int i = 0; i < photos.size(); i++) { // 默认全部未选中
            deletIndexs.add("0");
        }

        RecyclerView recycler = (RecyclerView)findViewById(R.id.id_recyclerview);
        deleteBtn = (Button)findViewById(R.id.photo_delete_btn);

        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0;i < deletIndexs.size(); i++) {
                    int delete = Integer.valueOf(deletIndexs.get(i));
                    if (delete == 1) {
                        // 删除文件
                        new File(photos.get(i)).delete();
                        photos.remove(i);
                        deletIndexs.set(i,"0");
                    }
                    adapter.notifyDataSetChanged();
                }

            }
        });


        int h = getWindowManager().getDefaultDisplay().getHeight();
        int w = getWindowManager().getDefaultDisplay().getWidth();

        // 设置布局管理器
//        layoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
//        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//            @Override
//            public int getSpanSize(int position) {
//                return 1;
//            }
//        });
//        recycler.setLayoutManager(layoutManager);
        recycler.setLayoutManager(new CustomLayoutManager(CustomLayoutManager.VERTICAL,w,h));
        // 添加分割线
//        recycler.addItemDecoration(new GridItemDecoration(3, getResources().getDimensionPixelSize(R.dimen.padding_middle), true));
        // 设置Item增加、移除动画
        recycler.setItemAnimator(new DefaultItemAnimator());
        // 设置Adapter
        recycler.setAdapter(adapter = new PhotoListAdapter());


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

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

    private void showPhotoPreview(int index) {
        Toast.makeText(PhotoListActivity.this,"Click" + index,Toast.LENGTH_LONG).show();
        Intent intent = new Intent(this,NewPhotoPreviewActivity.class);
        intent.putExtra("photos",(Serializable)photos);
        intent.putExtra("index",index);
        startActivity(intent);
    }

    /**Adapter*/
    public class PhotoListAdapter extends RecyclerView.Adapter<PhotoListAdapter.PhotoViewHolder> {

        /** ViewHolder */
        public class PhotoViewHolder extends RecyclerView.ViewHolder {
            public ImageView photoView;
            public ImageButton checkBtn;
            public PhotoViewHolder(View view) {
                super(view);
                photoView = (ImageView)view.findViewById(R.id.photo_list_iv);
                checkBtn = (ImageButton)view.findViewById(R.id.photo_list_checkBtn);
            }
        }

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

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

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

            holder.photoView.setImageBitmap(bitmap);

            holder.checkBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.check_none));
            int delete = Integer.valueOf(deletIndexs.get(position));
            if (delete == 1) {
                holder.checkBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.check_check));
            }

            holder.checkBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImageButton checkBtn = (ImageButton)v;
                    checkBtn.setSelected(!checkBtn.isSelected());
                    if (checkBtn.isSelected()) {
                        checkBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.check_check));
                        deletIndexs.set(position,"1");
                    } else  {
                        checkBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.check_none));
                        deletIndexs.set(position,"0");
                    }
                }
            });

            holder.photoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showPhotoPreview(position);
                }
            });
        }

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

    /**缩小图片*/
    public Bitmap scaleImage(String path,int width,int height) {
        Bitmap source = BitmapFactory.decodeFile(path);

//        int originWidth = source.getWidth();
//        int originHeight = source.getHeight();
//
//        int width = originWidth, height = originHeight;
//
//        if (originHeight > 1024 || originWidth > 1024)
//        {
//            if (originWidth > originHeight)
//            {
//                width = 1024;
//                height = originHeight * 1024 / originWidth;
//
//            }
//            else
//            {
//
//                height = 1024;
//                width = originWidth * 1024 / originHeight;
//
//            }
//        }
        Bitmap scaled = Bitmap.createScaledBitmap(source, width, height, true);
        return scaled;
    }
}
