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.load.engine.DiskCacheStrategy;
import com.danding.yang800.Config.ConfigUtil;
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 ShopInfoActivity extends BaseActivity {
    @BindView(R.id.shopinfo_shopIv)
    ImageView shopinfo_shopIv;
    @BindView(R.id.shopinfo_zxingIv)
    ImageView shopinfo_zxingIv;
    @BindView(R.id.shopinfo_layout)
    LinearLayout shopinfo_layout;
    @BindView(R.id.shopInfo_priceTv)
    TextView shopInfo_priceTv;
    @BindView(R.id.shopInfo_nameTv)
    TextView shopInfo_nameTv;
    private PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_shop_info);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        String goodsUrl = intent.getStringExtra("goodsUrl");
        String goodsPrice = intent.getStringExtra("goodsPrice");
        String goodsName = intent.getStringExtra("goodsName");
        shopInfo_priceTv.setText("￥" + goodsPrice);
        shopInfo_nameTv.setText(goodsName);
        Glide.with(this).load(ConfigUtil.getImageUrl(goodsUrl, 750, 773)).error(R.drawable.huisebeijing).diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(shopinfo_shopIv);
        Bitmap bitmap = ZXingUtils.Create2DCode(url, 238, 236);
        shopinfo_zxingIv.setImageBitmap(bitmap);
        shopinfo_layout.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(shopinfo_layout);
                boolean save = ImgUtils.saveImageToGallery(ShopInfoActivity.this, bmap);
                if (save) {
                    ToastUtil.show(ShopInfoActivity.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.shopinfo_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.shopinfo_layout:
                finish();
                overridePendingTransition(0, 0);
                break;
        }
    }
}
