package com.tofly.ghrq_route.common.ui.view;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.mgq.base.utils.toast.ToastUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.polites.android.GestureImageView;
import com.polites.android.GestureImageViewTouchListener;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.common.util.image.ImageUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BigImageView extends LinearLayout {
    @BindView(R.id.bigimage_imageview)
    GestureImageView imgControl;
    @BindView(R.id.loading)
    ProgressBar loading;

    private Activity mContext;
    private String picpath;
    private Bitmap bmp;
    private ImageLoader loader;

    private OnMovingListener onMovingListener = null;

    public interface OnMovingListener{
        void  startDrag();
        void  stopDrag();
    }

    public BigImageView(Activity context, String picPath, OnMovingListener movingListener) {
        super(context);

        mContext = context;
        picpath = picPath;
        onMovingListener = movingListener;

        initAttrs();
        initView();
        initListener();

        //imgControl.setOnMovingListener(movingListener);
    }

    private void initAttrs() {

    }

    private void initView() {
        inflate(mContext, R.layout.ui_bigimage, this);
        ButterKnife.bind(this);
        init();
    }

    private void initListener() {
        imgControl.setClickable(true);
        imgControl.setOnClickListener(v -> {
            boolean isZoomed = false;
            GestureImageViewTouchListener touchListener = imgControl.getGestureImageViewTouchListener();
            if (touchListener != null){
                isZoomed = touchListener.isZoomed();
            }
            if (!isZoomed) {
                mContext.finish();
            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
    }

    private void init(){
        if (picpath == null)
            return;

        loading.setVisibility(View.VISIBLE);

        if (picpath.substring(0, 4).equals("http")) {

            loader = ImageLoader.getInstance();
            DisplayImageOptions defaultDisplayImageOptions = new DisplayImageOptions.Builder().considerExifParams(true)
                    .resetViewBeforeLoading(false).cacheInMemory(true).bitmapConfig(Bitmap.Config.ARGB_8888)
                    .delayBeforeLoading(200).imageScaleType(ImageScaleType.EXACTLY)
                    .build();
            loader.displayImage(picpath, imgControl, defaultDisplayImageOptions, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String s, View view) {
                    loading.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String s, View view, FailReason failReason) {
                    loading.setVisibility(View.GONE);
                    ToastUtil.showToast(mContext, "图片加载失败");
                }

                @Override
                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                    bmp = bitmap;
                    imgControl.setImageBitmap(bmp);
                    Rect frame = new Rect();
                    mContext.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
                    int statusBarHeight = frame.top;
                    int screenW = mContext.getWindowManager().getDefaultDisplay().getWidth();
                    int screenH = mContext.getWindowManager().getDefaultDisplay().getHeight() - statusBarHeight;

                    loading.setVisibility(View.GONE);
                    if (bmp != null) {
                        imgControl.setImageBitmap(bmp);
                        /*imgControl.imageInit(bmp, screenW, screenH, statusBarHeight, new ImageControl.ICustomMethod() {
                            @Override
                            public void customMethod(Boolean currentStatus) {
                                if (currentStatus == true){
                                    getParent().requestDisallowInterceptTouchEvent(true);
                                }
                            }
                        });*/
                    } else {
                        ToastUtil.showToast(mContext, "图片加载失败");
                    }
                }

                @Override
                public void onLoadingCancelled(String s, View view) {
                    loading.setVisibility(View.GONE);
                }
            });
        } else {//不是网络地址就是本地图片路径，从本地加载
            bmp = ImageUtil.loadImageFromFilePath(picpath);
            imgControl.setImageBitmap(bmp);
            Rect frame = new Rect();
            mContext.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
            int statusBarHeight = frame.top;
            int screenW = mContext.getWindowManager().getDefaultDisplay().getWidth();
            int screenH = mContext.getWindowManager().getDefaultDisplay().getHeight() - statusBarHeight;


            loading.setVisibility(View.GONE);
            if (bmp != null) {
                imgControl.setImageBitmap(bmp);
                /*imgControl.imageInit(bmp, screenW, screenH, statusBarHeight, new ImageControl.ICustomMethod() {
                    @Override
                    public void customMethod(Boolean currentStatus) {

                    }
                });*/
            } else {
                ToastUtil.showToast(mContext, "图片加载失败");
            }
        }
    }

/*    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                imgControl.mouseDown(event);
                *//*if (onMovingListener != null)
                    onMovingListener.startDrag();*//*
                break;
            case MotionEvent.ACTION_POINTER_DOWN: // 非第一个点按下
                imgControl.mousePointDown(event);
                break;
            case MotionEvent.ACTION_MOVE:
                imgControl.mouseMove(event);
                break;
            case MotionEvent.ACTION_UP:
                imgControl.mouseUp();
                *//*if (onMovingListener != null)
                    onMovingListener.stopDrag();*//*
                break;

        }

        return super.onTouchEvent(event);
    }*/
}
