package com.caliverse.admin.domain.adminlog;


import com.caliverse.admin.domain.dao.admin.HistoryMapper;
import com.caliverse.admin.domain.entity.HISTORYTYPE;
import com.caliverse.admin.global.common.utils.CommonUtils;
import com.caliverse.admin.global.component.AdminApplicationContextProvider;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;


@Slf4j
public abstract class AdminLogBase implements IAdminLog{


    private final HistoryMapper historyMapper;
    private static ApplicationContext context;

    private final HISTORYTYPE historyType;

    protected Map<String, Object> map;

    @Getter
    protected JSONObject jsonContentObject = new JSONObject();


    public AdminLogBase(HISTORYTYPE historyType) {

        this.historyMapper = AdminApplicationContextProvider.getContext().getBean(HistoryMapper.class);
        this.historyType = historyType;

        jsonContentObject = new JSONObject();
        this.map = new HashMap<>();

        makeDefaultMap();
    }

    //protected abstract void saveAdminLog();


    private void makeDefaultMap(){


        Long adminId = 0L;
        String adminName = "";
        String adminMail = "";
        HISTORYTYPE type = HISTORYTYPE.NONE;
        try {
            adminId = CommonUtils.getAdmin().getId();
            adminName = CommonUtils.getAdmin().getName();
            adminMail = CommonUtils.getAdmin().getEmail();
        }
        catch (Exception e){
            log.error("makeDefaultMap getAdmin() null error message : {}", e.getMessage());
        }

        this.map.put("adminId", adminId);
        this.map.put("name", adminName);
        this.map.put("mail", adminMail);
        this.map.put("type", historyType);
    }

    @Override
    public void saveLogToDB(){
        this.map.put("content", jsonContentObject.toString());
        historyMapper.saveLog(map);
    }



}
