package com.module.pay.nayax;


import android.util.Log;

import com.module.pay.nayax.callback.CreditCardCallback;
import com.quyunshuo.androidbaseframemvvm.common.util.XLogUtil;

public class CreditCardPresenter implements CreditCardContract<CreditCardCallback> {
    //---------------线程 start------------------------------
//    private ScheduledThreadPoolExecutor scheduled;
    private static final String TAG = "CreditCardPresenter";

    public void initScheduled() {
        stopScheduled();

    }

    public void stopScheduled() {
    }
    //---------------线程 end------------------------------

    private static CreditCardMain mCreditCardMain;
    private double coin;

    //-----------------单例模式 start---------------
    private CreditCardPresenter() {
    }

    private static CreditCardPresenter sCreditCardPresenter;

    public static CreditCardPresenter getCreditCardContract() {
        // todo 有可能是因为这里死锁了。
        if (sCreditCardPresenter == null) {
            synchronized (CreditCardContract.class) {
                XLogUtil.INSTANCE.d("StartCommunication: 开始信用卡支付了3");
                sCreditCardPresenter = new CreditCardPresenter();
                Log.d(TAG, "getCreditCardContract: 1");
                sCreditCardPresenter.initScheduled();
                Log.d(TAG, "getCreditCardContract: 2");
                mCreditCardMain = CreditCardMain.getCreditCardMain();
                Log.d(TAG, "getCreditCardContract: 3");
                mCreditCardMain.StartCommunication(1);
                Log.d(TAG, "getCreditCardContract: 4");
            }
        }
        return sCreditCardPresenter;
    }
    //-----------------单例模式 end---------------

    /**
     * 串口通信
     */
    private boolean isCommunication = false;

    @Override
    public void startCommunication(int i) {
        //得到开启的对象
//        mCreditCardMain = CreditCardMain.getCreditCardMain();
        if (mCreditCardMain != null) {
            if (!mCreditCardMain.is_ready()) {
                Log.d(TAG, "StartCommunication: 开始信用卡支付了2");
                mCreditCardMain.StartCommunication(i);
                XLogUtil.INSTANCE.d("start111111StartCommunication: 执行一次了。"+i);
            }
        }
    }

    @Override
    public void stopCommunication(int i) {
        //得到开启的对象
//        mCreditCardMain = CreditCardMain.getCreditCardMain();
        if (mCreditCardMain != null) {
            if (!mCreditCardMain.is_ready()) {
                mCreditCardMain.stopCommunication(i);
            }
        }
    }

    //false:为不连接
    //true:为连接
    @Override
    public boolean is_ready() {
        XLogUtil.INSTANCE.d( "is_ready1: "+mCreditCardMain.is_read1());
        //todo 主要去测，如果已经连接，那么我们什么都不用处理了。也不需要反复打开
        XLogUtil.INSTANCE.d("is_ready2: "+mCreditCardMain.is_ready());
        return mCreditCardMain.is_ready();
    }

    /**
     * 结束串口通信
     */
    @Override
    public void StopPay() {
        if (mCreditCardMain != null) {
            mCreditCardMain.StopPay();
        }
    }

    /**
     * 注册UI的回调接口
     */
    @Override
    public void registerViewCallback(CreditCardCallback creditCardCallback) {

    }

    /**
     * 取消注册UI的回调接口
     */
    @Override
    public void unRegisterViewCallback(CreditCardCallback creditCardCallback) {

    }

    /**
     * 等待刷卡：nayax支付刷卡的第一步。
     */
    @Override
    public void startThreadTimer() {
//        scheduled.schedule(new Runnable() {
//            @Override
//            public void run() {
        if (mCreditCardMain != null ) {
            mCreditCardMain.startThreadTimer();
        }
//            }
//        }, 100, TimeUnit.MILLISECONDS);


    }

    /**
     * 取消本次等待刷卡
     */
    @Override
    public void CanselSession() {
//        scheduled.schedule(new Runnable() {
//            @Override
//            public void run() {
        if (mCreditCardMain != null ) {
            mCreditCardMain.CanselSession();
        }
//            }
//        }, 0, TimeUnit.MILLISECONDS);

    }

    /**
     * 交易结束后关闭
     */
    @Override
    public void CloseSession() {
//        scheduled.schedule(new Runnable() {
//            @Override
//            public void run() {
        if (mCreditCardMain != null ) {
            mCreditCardMain.CloseSession();
        }
//            }
//        }, 0, TimeUnit.MILLISECONDS);

    }

    /**
     * 发起支付
     */
    public void vendRequest(){
        if (mCreditCardMain != null) {
            mCreditCardMain.vendRequest();
        }
    }

    /**
     * 拿到商品数据
     * 相当设置价格
     * @param coin 硬币
     */
    @Override
    public void setNameAndPrice(double coin,int code) {
        this.coin = coin;
        this.setCommodity_price(coin,code);
    }
    /**
     * 相当支付成功后写入
     * @param
     */
    @Override
    public void nayaxCashSale(int price,int code) {
        if (mCreditCardMain != null) {
            mCreditCardMain.cashSale(code,price);
        }
    }


    /**
     * 設置信用卡所需要支付的數據
     */
    @Override
    public void setCommodity_price(double coin,int code) {
        Constant.setCommodity_price(coin);
        Constant.setCommodity_code(code);
//        ToastUtil.showToast(coin+"da");
    }




}