package com.danding.yang800;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.danding.yang800.Util.ImgUtils;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.ZXingUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InviteShareActivity extends BaseActivity {
    @BindView(R.id.inviteShare_zxIv)
    ImageView inviteShare_zxIv;
    @BindView(R.id.inviteShare_layout)
    LinearLayout inviteShare_layout;
    private PopupWindow popupWindow;
    @BindView(R.id.invite_scoll)
    LinearLayout invite_scoll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_invite_share);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        Bitmap bitmap = ZXingUtils.Create2DCode(url, 305, 305);
        inviteShare_zxIv.setImageBitmap(bitmap);
        invite_scoll.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showWindow();
                return false;
            }
        });
    }

    private void showWindow() {
        View contentView = LayoutInflater.from(this).inflate(
                R.layout.layout_popupwindows_picture, null);
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        TextView popupwindows_camera = (TextView) contentView
                .findViewById(R.id.popupwindows_camera);
        popupwindows_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bmap = createViewBitmap(invite_scoll);
                boolean save = ImgUtils.saveImageToGallery(InviteShareActivity.this, bmap);
                if (save) {
                    ToastUtil.show(InviteShareActivity.this, "保存成功");
                    popupWindow.dismiss();
                    finish();
                } else {
                    popupWindow.dismiss();
                }
            }
        });
        TextView tv_popupwindows_off = (TextView) contentView
                .findViewById(R.id.tv_popupwindows_off);
        tv_popupwindows_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
    }

    public Bitmap createViewBitmap(View v) {
        Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        v.draw(canvas);
        return bitmap;
    }

    @OnClick({R.id.invite_scoll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.invite_scoll:
                finish();
                overridePendingTransition(0, 0);
                break;

        }
    }
}
