package com.fzm.mall.server.admin.config;

import cn.fzm.chain.simplesdk.client.ParaChainClient;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.admin.auth_center.enums.status.AdminRoleStatusEnum;
import com.fzm.mall.server.admin.auth_center.enums.type.AdminRoleTypeEnum;
import com.fzm.mall.server.admin.auth_center.model.AdminRole;
import com.fzm.mall.server.admin.auth_center.service.IAdminRoleService;
import com.fzm.mall.server.admin.block_chain.util.ChainUtil;
import com.fzm.mall.server.admin.constant.MALLGlobalConfig;
import com.fzm.mall.server.admin.finance_center.enums.status.PlatformIntegralStatusEnum;
import com.fzm.mall.server.admin.finance_center.enums.type.PlatformIntegralTypeEnum;
import com.fzm.mall.server.admin.finance_center.model.PlatformIntegral;
import com.fzm.mall.server.admin.finance_center.service.IPlatformIntegralService;
import com.fzm.mall.server.admin.home_page.enums.status.AdminStatusEnum;
import com.fzm.mall.server.admin.home_page.enums.status.IsDeleteEnum;
import com.fzm.mall.server.admin.home_page.enums.status.MerchantRegisterEnum;
import com.fzm.mall.server.admin.home_page.enums.type.AdminTypeEnum;
import com.fzm.mall.server.admin.home_page.model.Admin;
import com.fzm.mall.server.admin.home_page.model.Merchant;
import com.fzm.mall.server.admin.home_page.service.IAdminService;
import com.fzm.mall.server.admin.home_page.service.IMerchantService;
import com.fzm.mall.server.admin.system.model.SysConfig;
import com.fzm.mall.server.admin.system.service.ISysConfigService;
import com.fzm.mall.server.admin.util.OSSUtil;
import com.fzm.mall.server.admin.util.SerialIdsUtil;
import com.fzm.mall.server.admin.util.Sha256Util;
import com.fzm.mall.server.admin.util.UUIDUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;

/**
 * @author wangp
 * @date 2021/3/29 15:58
 * @description 初始化配置
 * @since JDK 1.8
 */
@Slf4j
@Configuration
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class InitConfig {

    private final IPlatformIntegralService platformIntegralService;
    private final IAdminRoleService adminRoleService;
    private final IAdminService adminService;
    private final IMerchantService merchantService;
    private final ISysConfigService sysConfigService;
    private final ParaChainClient paraChainClient;

    private static String abi = "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"bool\",\"name\":\"approved\",\"type\":\"bool\"}],\"name\":\"ApprovalForAll\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256[]\",\"name\":\"ids\",\"type\":\"uint256[]\"},{\"indexed\":false,\"internalType\":\"uint256[]\",\"name\":\"values\",\"type\":\"uint256[]\"}],\"name\":\"TransferBatch\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"TransferSingle\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"string\",\"name\":\"value\",\"type\":\"string\"},{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"}],\"name\":\"URI\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"}],\"name\":\"balanceOf\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address[]\",\"name\":\"accounts\",\"type\":\"address[]\"},{\"internalType\":\"uint256[]\",\"name\":\"ids\",\"type\":\"uint256[]\"}],\"name\":\"balanceOfBatch\",\"outputs\":[{\"internalType\":\"uint256[]\",\"name\":\"\",\"type\":\"uint256[]\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"tokenId\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"burnToken\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"uint256[]\",\"name\":\"tokenIds\",\"type\":\"uint256[]\"},{\"internalType\":\"uint256[]\",\"name\":\"amounts\",\"type\":\"uint256[]\"}],\"name\":\"burnTokenBatch\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"tokenId\",\"type\":\"uint256\"}],\"name\":\"getTokenInfo\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"}],\"name\":\"getUserTokens\",\"outputs\":[{\"internalType\":\"uint256[]\",\"name\":\"\",\"type\":\"uint256[]\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"}],\"name\":\"isApprovedForAll\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"tokenId\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"mintToken\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"uint256[]\",\"name\":\"tokenIds\",\"type\":\"uint256[]\"},{\"internalType\":\"uint256[]\",\"name\":\"amounts\",\"type\":\"uint256[]\"}],\"name\":\"mintTokenBatch\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256[]\",\"name\":\"ids\",\"type\":\"uint256[]\"},{\"internalType\":\"uint256[]\",\"name\":\"amounts\",\"type\":\"uint256[]\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"safeBatchTransferFrom\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"safeTransferFrom\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"internalType\":\"bool\",\"name\":\"approved\",\"type\":\"bool\"}],\"name\":\"setApprovalForAll\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"tokenId\",\"type\":\"uint256\"},{\"internalType\":\"string\",\"name\":\"tokenInfo\",\"type\":\"string\"}],\"name\":\"setTokenInfo\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes4\",\"name\":\"interfaceId\",\"type\":\"bytes4\"}],\"name\":\"supportsInterface\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"tokenId\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"transferToken\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"uri\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"}]";
    private static String cAddr = "15DtbSyS4eVFxdF87jmrcAdXMSAwXEHVLW";


    @PostConstruct
    public void init() {
        //上传默认头像
        String adminHeadImg = "";
        SysConfig headImg = sysConfigService.getOne(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getDictKey, "admin_head_img"));
        if (headImg == null) {
            ClassPathResource resource = new ClassPathResource("template/head_img.png");
            InputStream inputStream = null;
            try {
                inputStream = resource.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String logoPath = OSSUtil.uploadStream(inputStream, "manage/img/" + SerialIdsUtil.createDateStr() + "/", SerialIdsUtil.createFileName(".png"));
            SysConfig sysConfig = new SysConfig();
            sysConfig.setConfigId(UUIDUtil.getUUID());
            sysConfig.setDictKey("admin_head_img");
            sysConfig.setDictValue(logoPath);
            sysConfig.setValidState(1);
            sysConfig.setStatus(1);
            sysConfig.setComm("管理员默认头像");
            sysConfigService.save(sysConfig);
            adminHeadImg = logoPath;
        } else {
            adminHeadImg = headImg.getDictValue();
        }

        //初始化平台默认角色
        AdminRole adminRole1 = new AdminRole(
                "平台默认角色",
                AdminRoleTypeEnum.PLATFORM_DEFAULT.getType(),
                AdminRoleStatusEnum.NORMAL.getStatus(),
                JSON.toJSONString(MALLGlobalConfig.PLAT_DEFAULT_AUTH),
                "平台默认角色"
        );
        adminRoleService.initRole(adminRole1);

        //初始化商户默认角色
        AdminRole adminRole2 = new AdminRole(
                "商户默认角色",
                AdminRoleTypeEnum.MERCHANT_DEFAULT.getType(),
                AdminRoleStatusEnum.NORMAL.getStatus(),
                JSON.toJSONString(MALLGlobalConfig.MERCHANT_DEFAULT_AUTH),
                "商户默认角色"
        );
        adminRoleService.initRole(adminRole2);

        //初始化平台商户默认角色
        AdminRole adminRole3 = new AdminRole(
                "平台商户默认角色",
                AdminRoleTypeEnum.PLATFORM_MERCHANT_DEFAULT.getType(),
                AdminRoleStatusEnum.NORMAL.getStatus(),
                JSON.toJSONString(MALLGlobalConfig.PLATFORM_MERCHANT_DEFAULT_AUTH),
                "平台商户默认角色"
        );
        adminRoleService.initRole(adminRole3);

        //初始化平台商户
        Merchant merchant = new Merchant(
                MALLGlobalConfig.PLATFORM_ADMIN_MERCHANT_ID,
                MALLGlobalConfig.PLATFORM_MERCHANT_PREFIX,
                MALLGlobalConfig.PLATFORM_ADMIN_MERCH,
                MALLGlobalConfig.PLATFORM_MERCHANT_NAME,
                MerchantRegisterEnum.NORMAL.getStatus(),
                IsDeleteEnum.NO.getStatus()
        );
        merchantService.initPlatformMerchant(merchant);

        //初始化平台管理员账号
        Admin admin1 = new Admin(
                MALLGlobalConfig.PLATFORM_MERCHANT_ID,
                adminRoleService.getByType(AdminRoleTypeEnum.PLATFORM_DEFAULT.getType()).getRoleId(),
                adminHeadImg,
                MALLGlobalConfig.PLATFORM_ADMIN,
                Sha256Util.encode(MALLGlobalConfig.INIT_PASSWORD),
                "超级管理员",
                AdminTypeEnum.PLATFORM_ADMINISTRATOR.getType(),
                AdminStatusEnum.NORMAL.getStatus()
        );
        adminService.initAdmin(admin1);

        //初始化平台管理员商户账号
        Admin admin2 = new Admin(
                MALLGlobalConfig.PLATFORM_ADMIN_MERCHANT_ID,
                adminRoleService.getByType(AdminRoleTypeEnum.PLATFORM_MERCHANT_DEFAULT.getType()).getRoleId(),
                adminHeadImg,
                MALLGlobalConfig.PLATFORM_ADMIN_MERCH,
                Sha256Util.encode(MALLGlobalConfig.INIT_PASSWORD),
                "超级管理员",
                AdminTypeEnum.PLATFORM_MERCHANT.getType(),
                AdminStatusEnum.NORMAL.getStatus()
        );
        adminService.initAdmin(admin2);

        //初始化平台基础积分
        PlatformIntegral integral = new PlatformIntegral(
                MALLGlobalConfig.PLATFORM_BASE_INTEGRAL,
                "平台基础积分",
                BigDecimal.ONE,
                new BigDecimal(1000),
                new BigDecimal(1000),
                PlatformIntegralTypeEnum.BASE.getType(),
                PlatformIntegralStatusEnum.ABLE.getStatus()
        );
        platformIntegralService.initBaseIntegral(integral);
    }

}
