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

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ProgressBar;

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.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.common.ui.view.ImageControl;
import com.tofly.ghrq_route.common.util.image.ImageUtil;

/**
 *
 * Created by zombiexq on 2016/6/29.
 */
public class UiBigImage extends BaseUi {

    private ImageControl imgControl;
    private ProgressBar loading;
    private String picpath;
    private Bitmap bmp;
    private ImageLoader loader;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ui_bigimage);
        initView();
        initData();
        initListener();
    }

    private void initView() {
        imgControl = findViewById(R.id.bigimage_imageview);
        loading = findViewById(R.id.loading);
    }

    private void initData() {
        picpath = getIntent().getStringExtra(ImageUtil.CURRENT_PATH_KEY);
    }

    private void initListener(){

    }

    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        init();
    }

    private void init() {
        // tvTitle.setText("图片测试");
        // 这里可以为imgcontrol的图片路径动态赋值

        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.NONE)
                    .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);
                    toast("图片加载失败");
                }

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

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

                            }
                        });
                    } else {
                        toast("图片加载失败");
                    }
                }

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


            loading.setVisibility(View.GONE);
            if (bmp != null) {
                imgControl.imageInit(bmp, screenW, screenH, statusBarHeight, currentStatus -> {

                });
            } else {
                toast("图片加载失败");
            }
        }
    }

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

        }

        return super.onTouchEvent(event);
    }


}
