package com.xiaoshangxing.Private.QCW.tongxuededian;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xiaoshangxing.R;

import util.ImageTools;

public class AKyifudianActivity extends AppCompatActivity implements View.OnClickListener {
    private  PopupWindow fabuPop = null;
    private Button back;
    private TextView textTile;
    private ImageButton btn_more;
    private Toolbar tb_reward;
    private ImageButton ib_AK_zfhy;
    private ImageButton ib_AK_zfxyq;
    private ImageButton ib_AK_zfwx;
    private ImageButton ib_AK_zfwb;
    private ImageButton ib_AK_zfqq;
    private Button btn_surround_info_cancel;
    private LinearLayout ll_surround_info_zhuanfa;
    private RelativeLayout relativeLayoutTransprant;
    private RelativeLayout rl_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_akyifudian);
        initView();
        Intent k = getIntent();
        String data = k.getStringExtra("data");
        if (data!=null&&!data.equals("")) {
            if (data.equals("dialog")) {
                showShareFriend(this);
            }
        }
    }

    private void initView() {
        relativeLayoutTransprant = (RelativeLayout)findViewById(R.id.rl_akfz_transprant);
        back = (Button) findViewById(R.id.back);
        rl_main = (RelativeLayout)findViewById(R.id.rl_ak_main);
        textTile = (TextView) findViewById(R.id.textTile);
        btn_more = (ImageButton) findViewById(R.id.btn_more);
        tb_reward = (Toolbar) findViewById(R.id.tb_reward);
        ib_AK_zfhy = (ImageButton) findViewById(R.id.ib_AK_zfhy);
        ib_AK_zfxyq = (ImageButton) findViewById(R.id.ib_AK_zfxyq);
        ib_AK_zfwx = (ImageButton) findViewById(R.id.ib_AK_zfwx);
        ib_AK_zfwb = (ImageButton) findViewById(R.id.ib_AK_zfwb);
        ib_AK_zfqq = (ImageButton) findViewById(R.id.ib_AK_zfqq);
        btn_surround_info_cancel = (Button) findViewById(R.id.btn_surround_info_cancel);
        ll_surround_info_zhuanfa = (LinearLayout) findViewById(R.id.ll_surround_info_zhuanfa);

        back.setOnClickListener(this);
        btn_more.setOnClickListener(this);
        ib_AK_zfhy.setOnClickListener(this);
        ib_AK_zfxyq.setOnClickListener(this);
        ib_AK_zfwx.setOnClickListener(this);
        ib_AK_zfwb.setOnClickListener(this);
        ib_AK_zfqq.setOnClickListener(this);
        btn_surround_info_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                    finish();
                break;
            case R.id.btn_more:
                showRelese(v);
                break;
            case R.id.ib_AK_zfhy:
                Intent zfhy = new Intent(AKyifudianActivity.this,FenXiangHaoYou.class);
                zfhy.putExtra("data","dialog");
                startActivity(zfhy);
                break;
            case R.id.ib_AK_zfxyq:
                Intent releseDongTai = new Intent(AKyifudianActivity.this,ReleseDongTaiActivity.class);
                startActivity(releseDongTai);
                break;
            case R.id.ib_AK_zfwx:
                showZhuanFaDia(AKyifudianActivity.this,"微信");
                break;
            case R.id.ib_AK_zfwb:
                    showZhuanFaDia(AKyifudianActivity.this,"微博");
                break;
            case R.id.ib_AK_zfqq:
                showZhuanFaDia(AKyifudianActivity.this,"QQ");
                break;
            case R.id.btn_surround_info_cancel:
                relativeLayoutTransprant.setVisibility(View.GONE);
                back.setEnabled(true);
                btn_more.setEnabled(true);
                rl_main.setAlpha(1.0f);
                break;
        }
    }
    private void showRelese(View moreBtnView) {
        Log.d("我要发布","你好");
        LayoutInflater li = (LayoutInflater) AKyifudianActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View content = li.inflate(R.layout.pop_akclothes_relese, null);
        if (fabuPop == null) {
            fabuPop = new PopupWindow(content, ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
//            int i = 5 * fl.getMeasuredWidth()/18;
//            int a =	(new Double(0.25*layout.getMeasuredHeight())).intValue();
//
//            window = new PopupWindow(v, i,a);
            Button wyfb = (Button) content.findViewById(R.id.btn_pop_reward_relese_wyfb);
            Button yfb = (Button) content.findViewById(R.id.btn_pop_reward_relese_yfb);
            Button share = (Button)content.findViewById(R.id.btn_pop_reward_relese_share);
            Button sc = (Button) content.findViewById(R.id.btn_pop_reward_relese_sc);
            wyfb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        Intent intent = new Intent(AKyifudianActivity.this, AKyFaqiHuiHua.class);
                    startActivity(intent);
                    fabuPop.dismiss();
                }
            });

            share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    relativeLayoutTransprant.setVisibility(View.VISIBLE);
                    rl_main.setAlpha(0.6f);
                    fabuPop.dismiss();
                    back.setEnabled(false);
                    btn_more.setEnabled(false);

                }
            });
            yfb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent intent = new Intent(SchoolRewardActivity.this,MyRewardActiviy.class);
//                    startActivity(intent);
//                    fabuPop.dismiss();
                }
            });
            sc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent intent = new Intent(SchoolRewardActivity.this,RewardFavoriteActivity.class);
//                    startActivity(intent);
//                    fabuPop.dismiss();
                }
            });

        }
        fabuPop.setBackgroundDrawable(new BitmapDrawable());
        fabuPop.setFocusable(true);
        fabuPop.update();
        int popupWidth;
        int popupHeight;

        content.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupWidth = content.getMeasuredWidth();
        popupHeight = content.getMeasuredHeight();

        View parent = fabuPop.getContentView();


        int heightMoreBtnView = moreBtnView.getHeight();
        int widthMoreBtnView = moreBtnView.getWidth();
        WindowManager m = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        fabuPop.showAsDropDown(moreBtnView, -230,
               50);


	}

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK )
        {
            if (relativeLayoutTransprant.getVisibility()==View.VISIBLE){
                relativeLayoutTransprant.setVisibility(View.GONE);
                back.setEnabled(true);
                btn_more.setEnabled(true);
                rl_main.setAlpha(1.0f);
            }
            else {
                finish();
            }

        }

        return false;

    }
    public void showShareFriend(Context context) {
        final AlertDialog customDia = new AlertDialog.Builder(context).create();
        final View viewDia = LayoutInflater.from(context).inflate(R.layout.dialog_akyifu_share_friend, null);
        customDia.setView(viewDia);
        EditText et = (EditText)viewDia.findViewById(R.id.et_dialog_akyifu_share);
        ImageView iv = (ImageView)viewDia.findViewById(R.id.iv_yili);
        iv.setImageBitmap(ImageTools.bigdrawableToBitmap(getResources().getDrawable(R.mipmap.dian1)));
        Button open = (Button) viewDia.findViewById(R.id.btn_reward_list_dialog_send);

        Button cancel = (Button) viewDia.findViewById(R.id.btn_reward_list_dialog_cancel);


        customDia.setCanceledOnTouchOutside(false);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customDia.dismiss();
            }
        });
        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customDia.dismiss();
            }
        });
        customDia.setCanceledOnTouchOutside(false);

        customDia.show();
    }
    public void showZhuanFaDia(Context context,String name) {
        final AlertDialog customDia = new AlertDialog.Builder(context).create();
        final View viewDia = LayoutInflater.from(context).inflate(R.layout.dialog_fasong, null);
        customDia.setView(viewDia);
        TextView textView = (TextView)viewDia.findViewById(R.id.tv_dialog_ak_zhuanfa);

        Button open = (Button) viewDia.findViewById(R.id.btn_dialog_open);

        Button cancel = (Button) viewDia.findViewById(R.id.btn_dialog_cancel);


        textView.setText("“校上行”想要打开“"+name+"”");
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customDia.dismiss();
            }
        });
        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customDia.dismiss();
            }
        });
        customDia.setCanceledOnTouchOutside(false);

        customDia.show();
    }
}
