package com.gh.gamecenter.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.gh.gamecenter.common.R;
import com.gh.gamecenter.core.utils.DisplayUtils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class CropImageCustom extends RelativeLayout {

    private CropImageZoomView mZoomImageView;
    private CropImageBorderView mClipImageView;

    private int mHorizontalPadding = 20;

    private float mRatio = 1F; // 裁剪比例

    public CropImageCustom(Context context, AttributeSet attrs) {
        super(context, attrs);

        mZoomImageView = new CropImageZoomView(context);
        mClipImageView = new CropImageBorderView(context);

        android.view.ViewGroup.LayoutParams lp = new LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);

        this.addView(mZoomImageView, lp);
        this.addView(mClipImageView, lp);

        // 计算padding的px
        mHorizontalPadding = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, mHorizontalPadding, getResources()
                        .getDisplayMetrics());
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CropImageCustom);
        mHorizontalPadding = (int) ta.getDimension(R.styleable.CropImageCustom_horizontalPadding, mHorizontalPadding);
        ta.recycle();

        mZoomImageView.setHorizontalPadding(mHorizontalPadding, mRatio);
        mClipImageView.setHorizontalPadding(mHorizontalPadding, mRatio);
    }

    public void setCropRatio(float ratio) {
        this.mRatio = ratio;
        mZoomImageView.setHorizontalPadding(mHorizontalPadding, mRatio);
        mClipImageView.setHorizontalPadding(mHorizontalPadding, mRatio);
    }

    public void addAssistView(View view) {
        int width = DisplayUtils.getScreenWidth() - 2 * mHorizontalPadding;
        int height = (int) (width * mRatio);
        RelativeLayout.LayoutParams lp = new LayoutParams(width, height);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        this.addView(view, lp);
    }

    /**
     * 裁切图片
     *
     * @return
     */
    public Bitmap clip() {
        return mZoomImageView.clip();
    }

    public boolean savePicture(String path) {
        try {
            Bitmap clip = mZoomImageView.clip();
            try {
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(path)));
                clip.compress(Bitmap.CompressFormat.WEBP, 100, bos);
                bos.flush();
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public ImageView getCropImageZoomView() {
        return mZoomImageView;
    }

    public void setBitmap(Bitmap bitmap) {
        mZoomImageView.setImageBitmap(bitmap);
    }

}
