package com.android.chmo.receiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.android.chmo.app.VChatManager;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.VideoPriceRes;
import com.android.chmo.http.service.ModelService;
import com.google.gson.Gson;
import com.netease.nim.avchatkit.common.activity.ActivityManager;
import com.netease.nimlib.sdk.avchat.model.AVChatData;

/**
 * Created by Administrator on 2018/8/9.
 */

public class VChatReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d("xxxxx", "zzzzz");
        if (action.equals("android.chmo.action.INCOMING")) { // 接收到视频
            AVChatData data = (AVChatData) intent.getSerializableExtra("data");
            String name = intent.getStringExtra("name");
            inComing(data, name);
        } else if (action.equals("android.chmo.action.BEGIN_VIDEO")) { // 视频已连接
            String account = intent.getStringExtra("account");
            beginVideo(account);
        } else if (action.equals("android.chmo.action.END_VIDEO")) { // 视频结束
            endVideo();
        }
    }


    private void inComing(final AVChatData data, final String name) {

        ModelService.getModelVideoPrice(data.getAccount(), new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                VideoPriceRes res = new Gson().fromJson(result, VideoPriceRes.class);
                float vprice = 6;
                if (res.msg.equals("success")) {
                    vprice = res.vprice;
                }
                Activity activity = ActivityManager.getInstance().currentActivity();
                VChatManager.getInstance().showPromptDialog(activity, vprice, new VChatManager.VChatCallback() {
                    @Override
                    public void startVChat() {
                    }
                });
            }

            @Override
            public void onFailure(String error) {
                Activity activity = ActivityManager.getInstance().currentActivity();
                VChatManager.getInstance().showPromptDialog(activity, 6, new VChatManager.VChatCallback() {
                    @Override
                    public void startVChat() {
                    }
                });
            }
        });


    }

    private void beginVideo(String account) {
        ModelService.getModelVideoPrice(account, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                VideoPriceRes res = new Gson().fromJson(result, VideoPriceRes.class);
                if (res.msg.equals("success")) {
                    VChatManager.getInstance().requestBegin(res.modelpk);
                }
            }

            @Override
            public void onFailure(String error) {
            }
        });

    }

    private void endVideo() {
        VChatManager.getInstance().requestEnd();
    }
}
