package com.softwarebr.nunu.web.admin;

import com.brframework.commoncms.annatotion.AdminMenu;
import com.brframework.commoncms.annatotion.layout.EditAlertLayout;
import com.brframework.commoncms.annatotion.layout.EditLayout;
import com.brframework.commoncms.annatotion.layout.PageLayout;
import com.brframework.commoncms.annatotion.option.CmsOption;
import com.brframework.commoncms.core.Icons;
import com.brframework.commonweb.json.JSONResult;
import com.brframework.commonweb.json.Page;
import com.brframework.commonweb.json.PageParam;
import com.brframework.commonwebadmin.aop.annotation.AOLog;
import com.softwarebr.nunu.config.domain.ConfigService;
import com.softwarebr.nunu.domain.AgreementConfig;
import com.softwarebr.nunu.domain.ServiceTerms;
import com.softwarebr.nunu.dto.oper.AdvertisementParamDTO;
import com.softwarebr.nunu.entity.oper.Advertisement;
import com.softwarebr.nunu.entity.oper.Area;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.json.admin.oper.AdvertisementParam;
import com.softwarebr.nunu.json.admin.oper.AdvertisementQueryParam;
import com.softwarebr.nunu.json.admin.oper.AdvertisementResult;
import com.softwarebr.nunu.service.oper.AdvertisementService;
import com.softwarebr.nunu.service.oper.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

import static com.brframework.commondb.core.ControllerPageHandle.convertTo;
import static com.brframework.commondb.core.ControllerPageHandle.jpaPageConvertToPage;

/**
 * @author lilin
 * @date 2020-02-18 10:58:32
 */

@RestController
@Api(tags = "运营")
@AdminMenu(menuName = "运营管理", icon = Icons.TABLE)
@RequestMapping("/admin/access")
public class AdminOperController {

    @Autowired
    private AdvertisementService advertisementService;

    @Autowired
    private ConfigService<ServiceTerms> serviceTermsConfigService;

    @Autowired
    private ConfigService<AgreementConfig> agreementConfigService;

    @Autowired
    private AreaService areaService;

    @ApiOperation(value = "广告列表", notes = "广告列表", produces = "text/plain")
    @GetMapping("/v1/advertisement/page")
    @AdminMenu(menuName = "广告管理", order = 1)
    @PreAuthorize("hasRole('oper-advertisement-query')")
    @PageLayout(
            options = {
                    @CmsOption(name = "添加", uriMappingMethod = "advertisementCreateV1")
            },
            columnOptions = {
                    @CmsOption(condition = "{status}==0", name = "上架", uriMappingMethod = "advertisementUpperV1"),
                    @CmsOption(condition = "{status}==1", name = "下架", uriMappingMethod = "advertisementLowerV1"),
                    @CmsOption(name = "编辑", uriMappingMethod = "advertisementEditV1"),
                    @CmsOption(name = "删除", uriMappingMethod = "advertisementDelV1", alert = "确定删除该记录吗?")
            }
    )
    public JSONResult<Page<AdvertisementResult>> advertisementListV1(PageParam pageParam, AdvertisementQueryParam param) {
        Page<AdvertisementResult> page = jpaPageConvertToPage(advertisementService.page(pageParam, param), pageParam, AdvertisementResult.class);
        return JSONResult.ok(page);
    }

    @ApiOperation(value = "修改广告信息", notes = "修改广告信息", produces = "text/plain")
    @PostMapping("/v1/advertisement/{id}")
    @PreAuthorize("hasRole('oper-advertisement-edit-v1')")
    @AOLog
    @EditAlertLayout(
            paddingMappingMethod = "getAdvertisement"
    )
    public JSONResult advertisementEditV1(@PathVariable Long id, @Valid @RequestBody AdvertisementParam param) {
        advertisementService.updateAdvertisement(id, convertTo(param, AdvertisementParamDTO.class));
        return JSONResult.ok();
    }

    @ApiOperation(value = "添加广告信息", notes = "添加广告信息", produces = "text/plain")
    @PutMapping("/v1/advertisement/add")
    @PreAuthorize("hasRole('oper-advertisement-create-v1')")
    @AOLog
    @EditLayout
    public JSONResult advertisementCreateV1(@Valid @RequestBody AdvertisementParam param) {
        advertisementService.saveAdvertisement(convertTo(param, AdvertisementParamDTO.class));
        return JSONResult.ok();
    }

    @ApiOperation(value = "删除广告信息", notes = "删除广告信息", produces = "text/plain")
    @DeleteMapping("/v1/advertisement/{id}")
    @PreAuthorize("hasRole('oper-advertisement-del-v1')")
    @AOLog
    public JSONResult advertisementDelV1(@PathVariable Long id) {
        advertisementService.removeById(id);
        return JSONResult.ok();
    }

    @ApiOperation(value = "获取广告信息", notes = "获取广告信息", produces = "text/plain")
    @PreAuthorize("hasRole('oper-advertisement-query')")
    @GetMapping("/v1/advertisement/{id}")
    public JSONResult<Advertisement> getAdvertisement(@PathVariable Long id) {
        Advertisement advertisement = advertisementService.accessObject(id);
        return JSONResult.ok(advertisement);
    }

    @ApiOperation(value = "广告上架", notes = "广告上架", produces = "text/plain")
    @PreAuthorize("hasRole('oper-advertisement-upper-edit')")
    @PostMapping("/v1/advertisement/upper/{id}")
    @AOLog
    public JSONResult advertisementUpperV1(@PathVariable Long id) {
        advertisementService.advertisementUpperShelf(id);
        return JSONResult.ok();
    }

    @ApiOperation(value = "广告下架", notes = "广告下架", produces = "text/plain")
    @PreAuthorize("hasRole('oper-advertisement-lower-edit')")
    @PostMapping("/v1/advertisement/lower/{id}")
    @AOLog
    public JSONResult advertisementLowerV1(@PathVariable Long id) {
        advertisementService.advertisementLowerShelf(id);
        return JSONResult.ok();
    }


    @ApiOperation(value = "获取省级信息", notes = "获取省级信息", produces = "text/plain")
    @GetMapping("/v1/area/province")
    public JSONResult<List<Area>> getAreaParent() {
        List<Area> province = areaService.findProvince();
        return JSONResult.ok(province);
    }

    @ApiOperation(value = "获取市级/区县信息", notes = "获取市级/区县信息", produces = "text/plain")
    @GetMapping("/v1/area/city/{parentId}")
    public JSONResult<List<Area>> getAreaChild(@PathVariable Long parentId) {
        List<Area> province = areaService.findCity(parentId);
        return JSONResult.ok(province);
    }


    @ApiOperation(value = "服务条款", notes = "服务条款")
    @PostMapping("/v1/service/terms/config")
    @EditLayout(paddingMappingMethod = "getServiceTerms")
    @AdminMenu(menuName = "服务条款", order = 2)
    @PreAuthorize("hasRole('system-service-terms-config')")
    @AOLog
    public JSONResult postServiceTerms(@RequestBody ServiceTerms param) {
        serviceTermsConfigService.set(param);
        return JSONResult.ok();
    }

    @ApiOperation(value = "获取服务条款", notes = "获取服务条款")
    @PreAuthorize("hasRole('system-service-terms-config')")
    @GetMapping("/v1/service/terms/config")
    public JSONResult<ServiceTerms> getServiceTerms() {
        return JSONResult.ok(serviceTermsConfigService.get());
    }

    @ApiOperation(value = "用户协议", notes = "用户协议")
    @PostMapping("/v1/agreement/config")
    @EditLayout(paddingMappingMethod = "getServiceTerms")
    @AdminMenu(menuName = "用户协议", order = 3)
    @PreAuthorize("hasRole('system-agreement-config')")
    @AOLog
    public JSONResult postAgreement(@RequestBody AgreementConfig param) {
        agreementConfigService.set(param);
        return JSONResult.ok();
    }

    @ApiOperation(value = "获取用户协议", notes = "获取用户协议")
    @PreAuthorize("hasRole('system-agreement-config')")
    @GetMapping("/v1/agreement/config")
    public JSONResult<AgreementConfig> getAgreementConfig() {
        return JSONResult.ok(agreementConfigService.get());
    }


}