package ai.pai.lensman.briefs;

import android.content.Context;
import android.content.Intent;

import com.android.common.utils.NetworkUtil;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import ai.pai.lensman.App;
import ai.pai.lensman.R;
import ai.pai.lensman.activities.PriceSettingActivity;
import ai.pai.lensman.activities.WebViewActivity;
import ai.pai.lensman.base.BaseInteractor;
import ai.pai.lensman.bean.BriefsBean;
import ai.pai.lensman.bean.MessageBean;
import ai.pai.lensman.bean.SystemMessageBean;
import ai.pai.lensman.main.QueryBoxStatusInteractor;

public class BriefsPresenter implements BriefsContract.Presenter,BaseInteractor.InteractorListener<BriefsBean>,
        QueryBoxStatusInteractor.BoxStatusListener {

    private Context context;
    private BriefsContract.View briefsView;
    private BriefsInteractor interactor;
    private QueryBoxStatusInteractor boxStatusInteractor;

    private BriefsBean briefsBean;
    private String NULL_STR = "";
    private Timer timer;
    private MessageBean currentMsg;
    private SystemMessageBean currentSystemMsg;


    public BriefsPresenter(Context context,BriefsContract.View briefsView){
        this.context = context;
        this.briefsView = briefsView;
        interactor = new BriefsInteractor(this);
        boxStatusInteractor = new QueryBoxStatusInteractor(this);
    }

    @Override
    public void start() {
        interactor.startJob();
        boxStatusInteractor.startJob();
    }

    @Override
    public void stop() {
        interactor.cancelJob();
        boxStatusInteractor.cancelJob();
        if(timer!=null){
            timer.cancel();
        }
    }

    @Override
    public void onInteractSuccess(BriefsBean bean) {
        this.briefsBean = bean;
        briefsView.updateUserInfo(bean);
        briefsView.updatePriceInfo(App.getAppContext().getString(R.string.fixed_price,
                String.format("%.2f",bean.originPrice*1.0f/100), String.format("%.2f",bean.noMarkPrice*1.0f/100)));
        briefsView.updateMessageBar(getNextSystemMessage(),getNextOrderMessage());
        if(timer!=null){
            timer.cancel();
        }
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                briefsView.updateMessageBar(getNextSystemMessage(),getNextOrderMessage());
            }
        },1000,3000);
    }

    @Override
    public void onInteractFail(String errorMsg) {
        if(NetworkUtil.isWifiConnected(App.getAppContext())){
            interactor.startJob();
        }
    }

    private String getNextOrderMessage(){
        if(briefsBean == null || briefsBean.msgList==null || briefsBean.msgList.size()==0){
            return NULL_STR;
        }
        currentMsg =  briefsBean.msgList.get(new Random().nextInt(briefsBean.msgList.size()));
        currentMsg.content = formatOrderContent(currentMsg);
        return currentMsg.content;
    }

    private String formatOrderContent(MessageBean messageBean){
        return App.getAppContext().getString(R.string.customer_bought_photo_money,
                String.format("%.2f",messageBean.totalFee*1.0f/100));
    }

    private String getNextSystemMessage(){
        if(briefsBean == null || briefsBean.systemMsgList==null || briefsBean.systemMsgList.size()==0){
            return NULL_STR;
        }
        currentSystemMsg = briefsBean.systemMsgList.get(new Random().nextInt(briefsBean.systemMsgList.size()));
        return currentSystemMsg.content;
    }

    @Override
    public void onBoxStatusFetched(String boxNo, String boxStatus, String boxStatusCode) {
        briefsView.updateBoxInfo(boxNo,boxStatus+"("+boxStatusCode+")");
    }

    @Override
    public void clickCurrentOrderMsg() {
        if(currentMsg == null){
            return;
        }

    }

    @Override
    public void clickCurrentSysMsg() {
        if(currentSystemMsg == null){
            return;
        }
        if(currentSystemMsg.url == null){
            return;
        }
        Intent intent = new Intent(context,WebViewActivity.class);
        intent.putExtra("url",currentSystemMsg.url);
        intent.putExtra("title",currentSystemMsg.title);
        context.startActivity(intent);
    }

    @Override
    public void clickSettingPrice() {
        Intent intent = new Intent(context, PriceSettingActivity.class);
        intent.putExtra("origin",briefsBean.originPrice);
        intent.putExtra("nomark",briefsBean.noMarkPrice);
        context.startActivity(intent);
    }
}
