package com.neusoft.smart.pos.member.core;

import com.alibaba.fastjson.JSONObject;
import com.neusoft.smart.pos.dto.member.request.CheckMemberLevelRequest;
import com.neusoft.smart.pos.member.constants.MemberLevelConditionType;

public class RechargeBasedCondition extends MemberLevelCondition {

    private RechargeBasedCondition() {
    }

    private RechargeBasedCondition(Long rechargeAmount) {
        this.rechargeAmount = rechargeAmount;
    }

    private Long rechargeAmount;

    @Override
    protected JSONObject getCondition() {
        JSONObject condition = new JSONObject();
        condition.put("rechargeAmount", rechargeAmount);
        return condition;
    }

    @Override
    protected MemberLevelConditionType getType() {
        return MemberLevelConditionType.RECHARGE_BASED;
    }

    public static MemberLevelCondition.ConditionCreator getConditionCreator() {
        return new RechargeBasedCondition.ConditionCreator();
    }

    @Override
    public boolean isMatch(CheckMemberLevelRequest request) {
       return request.getLastRecharge()>=this.rechargeAmount;
    }

    private static class ConditionCreator extends MemberLevelCondition.ConditionCreator {

        @Override
        public MemberLevelCondition createInstance(JSONObject para) {
            return new RechargeBasedCondition(para.getLong("rechargeAmount"));
        }

        @Override
        public MemberLevelConditionType getMemberLevelConditionType() {
            return MemberLevelConditionType.RECHARGE_BASED;
        }

        @Override
        public MemberLevelCondition createDefaultInstance() {
            RechargeBasedCondition result = new RechargeBasedCondition();
            result.rechargeAmount = 0L;
            return result;
        }
    }


}
