package com.sheep.gamegroup.view.activity;

import android.app.AlertDialog;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kfzs.duanduan.fragment.FgtFriendExtractPage;
import com.kfzs.duanduan.fragment.PagerAdapter;
import com.sheep.gamegroup.absBase.BaseUMActivity;
import com.sheep.gamegroup.di.components.DaggerAskGetMoneyComponent;
import com.sheep.gamegroup.di.modules.AskGetMoneyModule;
import com.sheep.gamegroup.model.entity.FriendAndAwardEntity;
import com.sheep.gamegroup.presenter.AskGetMoneyContract;
import com.sheep.gamegroup.presenter.AskGetMoneyPresenter;
import com.sheep.gamegroup.util.StringUtils;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/19.
 */

public class AskGetMoneyAct extends BaseUMActivity implements UMShareListener, AskGetMoneyContract.View {


    @Inject
    AskGetMoneyPresenter mPresenter;
    @BindView(R.id.tv_size)
    TextView tvSize;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.rl_wx)
    RelativeLayout rlWx;
    @BindView(R.id.rl_qq)
    RelativeLayout rlQq;
    @BindView(R.id.rl_qr)
    RelativeLayout rlQr;
    @BindView(R.id.rl_link)
    RelativeLayout rlLink;
    @BindView(R.id.friend_extract_vp)
    ViewPager friend_extract_vp;
    @BindView(R.id.friend_extract_count_tv)
    TextView friend_extract_count_tv;
    @BindView(R.id.friend_extract_page_tv)
    TextView friend_extract_page_tv;
    @BindView(R.id.friend_extract_last_page_tv)
    TextView friend_extract_last_page_tv;
    @BindView(R.id.friend_extract_next_page_tv)
    TextView friend_extract_next_page_tv;

    private FriendAndAwardEntity mEntity;

    @Override
    protected int getLayoutId() {
        return R.layout.x_ask_getmoney_act_layout;
    }

    @Override
    public void initView() {
        TitleBarUtils.getInstance()
                .setTitle(this,"邀请赚钱")
                .setTitleFinish(this);
        DaggerAskGetMoneyComponent.builder().askGetMoneyModule(new AskGetMoneyModule(this))
                .netComponent(SheepApp.get(this).getNetComponent())
                .build()
                .inject(this);
        initFriendExtractList();
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        showProgress();
        mPresenter.getTask(null);
    }

    private void shareToWX() {
        shareToOther(1);
    }

    private void shareToQQ() {
        shareToOther(2);
    }

    private void shareToOther(int code) {
        UMImage thumb = new UMImage(this, R.drawable.icon);
        UMWeb web = new UMWeb(mEntity.getShare_link());
        web.setTitle(mEntity.getShare_title());//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription(mEntity.getShare_desc());//描述
        ShareAction shareAction = new ShareAction(AskGetMoneyAct.this);
        if (code == 1) {
            shareAction.setPlatform(SHARE_MEDIA.WEIXIN);
        } else {
            shareAction.setPlatform(SHARE_MEDIA.QQ);
        }
        shareAction.setCallback(this).withMedia(web).share();
    }


    private void shareToLink() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = View.inflate(this, R.layout.x_ask_reward_dialog, null);
        builder.setView(view);
        final AlertDialog dialog = builder.create();
//        dialog.getWindow().setDimAmount(0.1f);
//        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        TextView tv_link=view.findViewById(R.id.tv_link);
        tv_link.setText(mEntity.getShare_link());
        TextView withdrawal=view.findViewById(R.id.withdrawal);
        ImageView iv=view.findViewById(R.id.iv_close);
        dialog.show();
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        withdrawal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringUtils.CopyText(AskGetMoneyAct.this,mEntity.getShare_link());
                showToast("复制链接成功");
                dialog.dismiss();
            }
        });
    }




    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA share_media) {

    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {

    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {

    }

    @Override
    public void NetSuccess(int code, String msg) {

    }

    @Override
    public void NetError(int code, String errorMsg) {
        hideProgress();
        showToast(errorMsg);

    }

    @Override
    public void showTask(FriendAndAwardEntity entity) {
        hideProgress();
        mEntity=entity;
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvSize.setText(mEntity.getFriend_count()+"");
                tvMoney.setText(mEntity.getAward());
            }
        });

    }


    @OnClick({R.id.rl_wx, R.id.rl_qq, R.id.rl_qr, R.id.rl_link})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_wx:
                shareToWX();
                break;
            case R.id.rl_qq:
                shareToQQ();
                break;
            case R.id.rl_qr:
                ViewUtil.showQR(AskGetMoneyAct.this, mEntity.getShare_link());
                break;
            case R.id.rl_link:
                shareToLink();
                break;
        }
    }
    private SparseArray<FgtFriendExtractPage> mTestFragments;
    public void setFriendExtractCount(int count, int page, int size){
        friend_extract_count_tv.setText(getString(R.string.friend_extract_count, count));
        checkAddPage(page, size);
    }
    private void checkAddPage(int page, int size) {
        if(curPage+2 > page && size == FgtFriendExtractPage.per_page){//已经到最后一页面,且数据是满的，可以添加一页面
            int nextPage = page + 1;
            if(mTestFragments.get(nextPage) == null) {
//                if(BuildConfig.DEBUG) G.showToast("添加新的一页:" + nextPage);
                mTestFragments.put(nextPage, FgtFriendExtractPage.newInstance(nextPage));
                mPagerAdapter.notifyDataSetChanged();
            } else {
//                if(BuildConfig.DEBUG) G.showToast("不用添加新的一页:" + nextPage);
            }
        }
        if(size > FgtFriendExtractPage.per_page){
            if(BuildConfig.DEBUG) G.showToast("页面" + page+"的数据为："+size);
        }
    }

    private int curPage = 1;
    private PagerAdapter mPagerAdapter;
    private void initFriendExtractList(){
        mTestFragments = new SparseArray<>();
        mTestFragments.put(1, FgtFriendExtractPage.newInstance(1));
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager(), mTestFragments);
        friend_extract_vp.setAdapter(mPagerAdapter);
        friend_extract_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                curPage = i+1;
                friend_extract_page_tv.setText(""+curPage);
                checkAddPage(curPage,mTestFragments.get(curPage).getList().size());
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        friend_extract_last_page_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                friend_extract_vp.setCurrentItem(curPage - 2);
            }
        });
        friend_extract_next_page_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                friend_extract_vp.setCurrentItem(curPage);
            }
        });
    }
}
