package com.rv.share;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.base.utils.ui.image.round.RoundImageView;
import com.ruiwenliu.wrapper.base.BaseBean;
import com.ruiwenliu.wrapper.base.BaseStatusActivity;
import com.ruiwenliu.wrapper.weight.TitleView;
import com.rv.share.presenter.BillPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/********
 *
 * 海报界面
 * created by john
 */
public class BillActivity extends BaseStatusActivity<BillPresenter> {

    @BindView(R2.id.btn_share_bill)
    Button btnShare;
    @BindView(R2.id.img_bill_top)
    ImageView imgBillTop;
    @BindView(R2.id.ring_header)
    RoundImageView ringHeader;
    @BindView(R2.id.tv_phone)
    TextView tvPhone;
    @BindView(R2.id.img_qrcode)
    ImageView imgQrCode;
    @BindView(R2.id.carview)
    CardView cardView;

    private String imgUrl;
    private String codeUrl;

    public static Intent getIntent(Context var1, String var2, String var3) {
        Intent intent = new Intent(var1, BillActivity.class);
        intent.putExtra("codeUrl", var2);
        intent.putExtra("imgUrl", var3);
        return intent;
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_bill;
    }


    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {

        codeUrl = intent.getStringExtra("codeUrl");
        imgUrl = intent.getStringExtra("imgUrl");
        titleView.setVisibility(View.VISIBLE);
        titleView.setTitle("生成海报");
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {
        mPresenter.setHeader(ringHeader);
        mPresenter.setName(tvPhone);
        mPresenter.setCode(codeUrl, imgQrCode);
        mPresenter.setImgTop(imgUrl, imgBillTop);
    }

    @Override
    public void onShowResult(int requestType, BaseBean result) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R2.id.btn_share_bill, R2.id.btn_save_bill})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.btn_share_bill) {
            preShare();
        } else if (id == R.id.btn_save_bill) {
            mPresenter.saveBill(cardView);
        }
    }

    /**
     * 预备下载 进行 6.0权限检查
     */
    private void preShare() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            mPresenter.showShareDialog(cardView);
        } else {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                mPresenter.showShareDialog(cardView);
            } else {//申请权限
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1001:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    mPresenter.showShareDialog(cardView);
                } else {
                    Toast.makeText(getApplicationContext(), "授权失败,无法分享海报", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
