package com.zhaohaoting.framework.ui.utils.imagebrowsing;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhaohaoting.framework.R;
import com.zhaohaoting.framework.utils.DensityUtil;
import com.zhaohaoting.framework.utils.Logger;
import com.zhaohaoting.framework.utils.file.FileUtils;
import com.zhaohaoting.framework.utils.glid.GlideUtils;
import com.zhaohaoting.framework.utils.md5.MD5;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mine on 2015/9/24.
 */
public class ImagPagerUtil {
    private List<String> mPicList;
    private Context mActivity;
    private Dialog dialog;
    private LazyViewPager mViewPager;
    private int screenWidth;
    private TextView tv_img_current_index;
    private TextView tv_img_count;
    private TextView tv_content;
    private int position;
    private MyImagPagerAdapter myImagPagerAdapter;
    private Map<Integer,Bitmap> bitmapMap = new HashMap();

    public ImagPagerUtil(Context activity, List<String> mPicList, int position) {
        this.mPicList = mPicList;
        this.mActivity = activity;
        this.position = position;
        init();
    }

    public ImagPagerUtil(Context activity, String[] picarr, int position) {
        mPicList = new ArrayList<>();
        for (int i = 0; i < picarr.length; i++) {
            mPicList.add(picarr[i]);
        }
        this.mActivity = activity;
        this.position = position;
        init();
    }

    /**
     * 设置图片下方的文字
     * @param str
     */
    public void setContentText(String str) {
        if (!TextUtils.isEmpty(str)) {
            tv_content.setText(str);
        }
    }

    public void show() {
        dialog.show();
    }

    private void init() {
        dialog = new Dialog(mActivity, R.style.fullDialog);
        RelativeLayout contentView = (RelativeLayout) View.inflate(mActivity, R.layout.view_dialogpager_img, null);
        mViewPager = getView(contentView, R.id.view_pager);
        tv_img_current_index = getView(contentView, R.id.tv_img_current_index);
        tv_img_count = getView(contentView, R.id.tv_img_count);
        tv_content = getView(contentView, R.id.tv_content);
        dialog.setContentView(contentView);

        tv_img_count.setText(mPicList.size() + "");
        tv_img_current_index.setText("1");

        int size = mPicList.size();
        ArrayList<ImageView> imageViews = new ArrayList<>();
        ZoomImageView imageView = new ZoomImageView(mActivity);
        imageView.measure(0, 0);
        screenWidth = DensityUtil.getScreenWidth(mActivity);
        ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(screenWidth, DensityUtil.getScreenHeight(mActivity));
        imageView.setLayoutParams(marginLayoutParams);
//        imageView.setOnClickListener(new View.OnClickListener() {//如果不需要点击图片关闭的需求，可以去掉这个点击事件
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
        tv_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int currentItem = mViewPager.getCurrentItem();
//                ImageView childAt = (ImageView) mViewPager.getChildAt(currentItem);
//                Bitmap bitmap = PhotoUtil.getBitmap((Activity) mActivity, childAt, 0, 0, childAt.getWidth(), childAt.getHeight());
                Bitmap bitmap = bitmapMap.get(currentItem);
                try {
                    FileUtils.saveImageToGallery(bitmap, MD5.getStringMD5(mPicList.get(currentItem))+".jpg");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        for (int i = 0; i < size; i++) {
            imageViews.add(imageView);
        }
        initViewPager(imageViews);
    }

    private void initViewPager(ArrayList<ImageView> list) {
        mViewPager.setOnPageChangeListener(new LazyViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                tv_img_current_index.setText("" + (position + 1));
            }
        });

        myImagPagerAdapter = new MyImagPagerAdapter(list);
        mViewPager.setAdapter(myImagPagerAdapter);
        mViewPager.setCurrentItem(position, false);
    }

    class MyImagPagerAdapter extends PagerAdapter {
        ArrayList<ImageView> mList;

        public MyImagPagerAdapter(ArrayList<ImageView> mList) {
            this.mList = mList;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = mList.get(position);
            showPic(imageView, mPicList.get(position),position);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mList.get(position));
        }

        @Override
        public int getCount() {
            if (null == mList || mList.size() <= 0) {
                return 0;
            }
            return mList.size();
        }

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

    private void showPic(ImageView imageView, String url, int position) {
        imageView.setImageBitmap(null);
            GlideUtils.loadImage(mActivity,url,imageView);
        dialog.show();
    }


    @SuppressWarnings("unchecked")
    public static final <E extends View> E getView(View parent, int id) {
        try {
            return (E) parent.findViewById(id);
        } catch (ClassCastException ex) {
            Logger.e("ImagPageUtil", "Could not cast View to concrete class \n" + ex.getMessage());
            throw ex;
        }
    }
}
