package com.android.chmo.ui.activity.order;

import android.content.Intent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.app.ChmoApplication;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.HttpApi;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.OrderRes;
import com.android.chmo.http.response.PayOrderRes;
import com.android.chmo.http.service.OrderService;
import com.android.chmo.model.OrderInfo;
import com.android.chmo.ui.activity.me.RechargeActivity;
import com.android.chmo.utils.PixelUtils;
import com.android.chmo.utils.XUtilsImage;
import com.google.gson.Gson;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.CustomNotification;
import com.netease.nimlib.sdk.msg.model.CustomNotificationConfig;

import butterknife.BindView;
import butterknife.OnClick;


public class PayOrderActivity extends BaseActivity {

    @BindView(R.id.workName)
    TextView workNameView;
    @BindView(R.id.workAddress)
    TextView workAddrView;
    @BindView(R.id.image)
    ImageView imageView;
    @BindView(R.id.orderNum)
    TextView orderNumView;
    @BindView(R.id.modelName)
    TextView modelNameView;
    @BindView(R.id.workTime)
    TextView workTimeView;
    @BindView(R.id.totalPrice)
    TextView totalPriceView;
    @BindView(R.id.balance)
    TextView balanceView;

    private OrderInfo order;
    private  String orderPk;

    @Override
    protected int getContentResId() {
        return R.layout.activity_pay_order;
    }

    @Override
    public void initView() {
        int width = PixelUtils.getWindowWidth()/3;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) imageView.getLayoutParams();
        lp.width = width;
        lp.height = width;
        imageView.setLayoutParams(lp);

        orderPk = getIntent().getStringExtra("orderPk");

        getOrderInfo(orderPk);
    }

    private void fillView(){
        XUtilsImage.display(imageView, HttpApi.getImgUrl(order.hphoto),R.mipmap.def_head);
        workNameView.setText(order.sname);
        workAddrView.setText(order.addr);
        orderNumView.setText("订单号：" + order.ordernumber);
        modelNameView.setText(order.name);
        totalPriceView.setText("￥" + order.coin);
        workTimeView.setText("时长" + Math.round(Float.parseFloat(order.scount)) + "h");
        balanceView.setText("余额支付（剩余￥" + ChmoApplication.getApp().getLoginUser()
                .coin_a+ "）");
    }

    @OnClick(R.id.balanceClick)
    public void balanceClick(){
        Intent intent = new Intent(this,RechargeActivity.class);
        openPageForResult(intent, 1);
    }

    @OnClick(R.id.pay)
    public void pay(){
        showLoading();
        OrderService.payOrder(order.pk, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                PayOrderRes res = new Gson().fromJson(result, PayOrderRes.class);
                if(res.msg.equals("fail")) {
                    showToast(res.desc);
                }else {
                    sendPaySucNotify();
                    showToast("支付成功");
                    setResult(RESULT_OK);
                    finish();
                }
            }

            @Override
            public void onFailure(String error) {
                hideLoading();
                showToast("支付失败");
            }
        });
    }

    public void getOrderInfo(String orkerPk) {
        showLoading();
        OrderService.getOrder(orkerPk, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                OrderRes res = new Gson().fromJson(result,OrderRes.class);
                if(res.data != null) {
                    order = res.data;
                    fillView();
                }else {
                    showToast("获取订单失败");
                }
            }

            @Override
            public void onFailure(String error) {
                hideLoading();
                showToast("获取订单失败");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK && requestCode == 1) {
            getOrderInfo(orderPk);
        }
    }

    private void sendPaySucNotify() {
        CustomNotification command = new CustomNotification();
        command.setSessionId(order.model_memberpk);
        command.setSessionType(SessionTypeEnum.P2P);
        command.setSendToOnlineUserOnly(false);
        command.setContent("恭喜，您有新的订单");
        command.setApnsText("恭喜，您有新的订单");
        CustomNotificationConfig config = new CustomNotificationConfig();
        config.enablePush = true;
        config.enableUnreadCount = true;
        command.setConfig(config);
        NIMClient.getService(MsgService.class).sendCustomNotification(command);
    }
}
