package com.danding.yang800;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.os.PersistableBundle;
import android.support.v4.content.ContextCompat;
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.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.danding.yang800.Adapter.InviteNoteAdapter;
import com.danding.yang800.Presenter.InviteNoteActivityPresenter;
import com.danding.yang800.Util.GeneralUtil;
import com.danding.yang800.Util.ShareUtil;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.ToastUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.ShareBoardConfig;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;
import com.yanzhenjie.sofia.Sofia;

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

public class InviteNoteActivity extends BaseActivity {
    @BindView(R.id.invite_list)
    ListView invite_list;
    @BindView(R.id.invite_backTv)
    TextView invite_backTv;
    @BindView(R.id.invite_countTv)
    TextView invite_countTv;
    @BindView(R.id.invite_shopTv)
    TextView invite_shopTv;
    InviteNoteAdapter adapter;
    InviteNoteActivityPresenter presenter;
    private PopupWindow popupshareWindow;
    SharedPreferences sp;
    String title;
    @BindView(R.id.invite_shopIv)
    ImageView invite_shopIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        int type = StatusColorUtil.StatusBarLightMode(this);
//        if (type > 0) {
//            StatusColorUtil.setStatusBarColor(this, R.color.white);
////            StatusColorUtil.transparencyBar(this);
//            StatusColorUtil.StatusBarLightMode(this, type);
//        }
        setContentView(R.layout.activity_invite_note);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        ButterKnife.bind(this);
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        adapter = new InviteNoteAdapter(this);
        presenter = new InviteNoteActivityPresenter(this, adapter);
        presenter.getInfo();
    }

    @OnClick({R.id.invite_backTv, R.id.invite_shopTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.invite_backTv:
                finish();
                break;
            case R.id.invite_shopTv:
                showshareWindow();
                break;

        }
    }

    public void initData(int count, String inviteImgUrl) {
        invite_list.setAdapter(adapter);
        invite_countTv.setText(count + "人");
        Glide.with(this).load(inviteImgUrl).diskCacheStrategy(DiskCacheStrategy.ALL).into(invite_shopIv);
    }

    UMWeb web;

    //页面重绘调用
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        title = savedInstanceState.getString(title, "");
    }

    //页面销毁调用
    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        outState.putString("title", title);
    }

    public void showshareWindow() {
        View contentView = LayoutInflater.from(this).inflate(
                R.layout.layout_popupwindows_invite, null);
        popupshareWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        ImageView popupshareWindow_cancelIv = (ImageView) contentView.findViewById(R.id.popupshareWindow_cancelIv);
        RelativeLayout popupshareWindow_shareshop = (RelativeLayout) contentView.findViewById(R.id.popupshareWindow_shareshop);
        RelativeLayout popupshareWindow_zxing = (RelativeLayout) contentView.findViewById(R.id.popupshareWindow_zxing);
        popupshareWindow_cancelIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupshareWindow.dismiss();
            }
        });
        popupshareWindow_shareshop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO 跳转微信
                popupshareWindow.dismiss();
                ShareUtil.share(InviteNoteActivity.this, sp.getString("htmlUrl", ""), title,
                        "洋800是保税仓发货系统，提供进口正品货源，源头直采、合法清关、海关监管、正品保证！我邀请你来免费开店", "", 0);

            }
        });
        popupshareWindow_zxing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtil.getString(sp.getString("htmlUrl", ""))) {
                    Intent intent = new Intent(InviteNoteActivity.this, InviteShareActivity.class);
                    intent.putExtra("url", sp.getString("htmlUrl", ""));
                    startActivity(intent);
                    overridePendingTransition(0, 0);
                    popupshareWindow.dismiss();
                }
            }
        });
        popupshareWindow.setFocusable(true);
        popupshareWindow.setOutsideTouchable(true);
        popupshareWindow.setBackgroundDrawable(new BitmapDrawable());
        popupshareWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
    }

}
