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.RelativeLayout;
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.BitmapUtil;
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;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class ShopShareActivity extends AppCompatActivity {
    @BindView(R.id.shopshare_layout)
    RelativeLayout shopshare_layout;
    @BindView(R.id.shopshare_linearLayout)
    LinearLayout shopshare_linearLayout;
    @BindView(R.id.shopinfo_zxingIv)
    ImageView shopinfo_zxingIv;
    Bitmap bitmap;
    @BindView(R.id.shopShare_headIv)
    ImageView shopShare_headIv;
    @BindView(R.id.shopShare_storeNameTv)
    TextView shopShare_storeNameTv;
    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_share);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        String headUrl = intent.getStringExtra("headUrl");
        String storeName = intent.getStringExtra("storeName");
        shopShare_storeNameTv.setText(storeName);
        if (headUrl.equals("")) {
//            shopShare_headIv.setImageResource(R.drawable.main_myhead);
            Glide.with(this).load(R.drawable.main_myhead).error(R.drawable.huisebeijing)
                    .bitmapTransform(new RoundedCornersTransformation(this, 35, 0,
                            RoundedCornersTransformation.CornerType.ALL))
                    .into(shopShare_headIv);
        } else {
//            Glide.with(this).load(ConfigUtil.getImageUrl(headUrl, 156, 156)).error(R.drawable.huisebeijing).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(shopShare_headIv);
            Glide.with(this).load(ConfigUtil.getImageUrl(headUrl, 156, 156)).error(R.drawable.huisebeijing)
                    .bitmapTransform(new RoundedCornersTransformation(this, 35, 0,
                            RoundedCornersTransformation.CornerType.ALL))
                    .into(shopShare_headIv);
        }
//        if (backUrl.equals("")) {
//            shopShare_backIv.setImageResource(R.drawable.main_back);
//        } else {
//            Glide.with(this).load(ConfigUtil.getImageUrl(backUrl, 750, 382)).error(R.drawable.huisebeijing).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(shopShare_backIv);
//        }
        bitmap = ZXingUtils.Create2DCode(url, 305, 305);
        shopinfo_zxingIv.setImageBitmap(bitmap);
        shopshare_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(shopshare_layout);
                boolean save = ImgUtils.saveImageToGallery(ShopShareActivity.this, bmap);
                if (save) {
                    ToastUtil.show(ShopShareActivity.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.shopshare_linearLayout, R.id.shopshare_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.shopshare_layout:
                finish();
                overridePendingTransition(0, 0);
                break;
            case R.id.shopshare_linearLayout:
                finish();
                overridePendingTransition(0, 0);
                break;
        }
    }

}
