package com.danding.yang800;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alipay.sdk.app.PayTask;
import com.danding.yang800.General.PayResult;
import com.danding.yang800.Presenter.PayResultActivityPresenter;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.yanzhenjie.sofia.Sofia;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

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

public class PayResultActivity extends BaseActivity {
    @BindView(R.id.payResult_backTv)
    TextView payResult_backTv;
    @BindView(R.id.payResult_inShopTv)
    TextView payResult_inShopTv;
    @BindView(R.id.payResult_payTv)
    TextView payResult_payTv;
    @BindView(R.id.payResult_Iv)
    ImageView payResult_Iv;
    @BindView(R.id.payResult_resultTv)
    TextView payResult_resultTv;
    @BindView(R.id.payResult_priceIntTv)
    TextView payResult_priceIntTv;
    @BindView(R.id.payResult_priceTv)
    TextView payResult_priceTv;
    @BindView(R.id.payResult_addressTv)
    TextView payResult_addressTv;
    @BindView(R.id.payResult_numberTv)
    TextView payResult_numberTv;
    @BindView(R.id.payResult_nameTv)
    TextView payResult_nameTv;
    String orderInfo = "";
    String address = "";
    String name = "";
    String number = "";
    PayResultActivityPresenter presenter;
    private static final int SDK_PAY_FLAG = 111;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        int type = StatusColorUtil.StatusBarLightMode(this);
//        if (type > 0) {
//            StatusColorUtil.setStatusBarColor(this, R.color.white);
//            StatusColorUtil.StatusBarLightMode(this, type);
//        }
        setContentView(R.layout.activity_pay_result);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        ButterKnife.bind(this);
        presenter = new PayResultActivityPresenter(this);
        Intent intent = getIntent();
        String result = intent.getStringExtra("result");
        orderInfo = intent.getStringExtra("orderInfo");
        address = intent.getStringExtra("address");
        name = intent.getStringExtra("name");
        number = intent.getStringExtra("number");
        payResult_priceIntTv.setText(sp.getString("priceInt", ""));
        payResult_priceTv.setText(sp.getString("price", ""));
        payResult_addressTv.setText("收货地址：" + address);
        payResult_nameTv.setText("收货人：" + name);
        payResult_numberTv.setText(number);
        if (result.equals("success")) {
            payResult_Iv.setImageResource(R.drawable.paysuccess);
            payResult_resultTv.setText("支付成功");
            Timer timer = new Timer();
            TimerTask tast = new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            MobclickAgent.onProfileSignIn(sp.getInt("userId", 999999999)+"");
                            startActivity(MainActivity.class);
                        }
                    });
                }
            };
            timer.schedule(tast, 1 * 1000);
        } else {
            payResult_Iv.setImageResource(R.drawable.payfail);
            payResult_payTv.setVisibility(View.VISIBLE);
            payResult_resultTv.setText("支付失败");
        }

    }

    @OnClick({R.id.payResult_backTv, R.id.payResult_payTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.payResult_backTv:
                finish();
                break;
            case R.id.payResult_payTv:
                aliPay(orderInfo);
                break;

        }
    }

    Handler handler = new Handler() { // handle
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SDK_PAY_FLAG:
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
//                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    LogUtil.d(resultStatus + "resultStatus");
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        MobclickAgent.onProfileSignIn(sp.getInt("userId", 999999999)+"");
                        startActivity(MainActivity.class);
                    } else {
                        ToastUtil.show(PayResultActivity.this, "支付失败");
                    }
                    break;

            }
        }
    };

    public void aliPay(String result) {
        final String orderInfo;   // 订单信息
        try {


            byte[] b = result.getBytes("UTF-8");
            orderInfo = new String(StringUtil.base64Decrypt(b), "UTF-8");
            Runnable payRunnable = new Runnable() {

                @Override
                public void run() {
                    PayTask alipay = new PayTask(PayResultActivity.this);
                    Map<String, String> result = alipay.payV2(orderInfo, true);
                    Message msg = new Message();
                    msg.what = SDK_PAY_FLAG;
                    msg.obj = result;
                    handler.sendMessage(msg);
                }
            };
            // 必须异步调用
            Thread payThread = new Thread(payRunnable);
            payThread.start();
        } catch (Exception e) {

        }
    }
}
