package com.ruoyi.controller;

import com.ruoyi.annotations.UserLoginToken;
import com.ruoyi.controller.req.AddressInsertReq;
import com.ruoyi.controller.req.AddressUpdateReq;
import com.ruoyi.frequency.malladdress.service.MallAddressService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.PageBasic;
import com.ruoyi.vo.RyController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@CrossOrigin
@RestController
@RequestMapping("/api/address")
@Api(tags = "用户地址")
public class ApiAddressController extends RyController {


    @Autowired
    private MallAddressService addressService;

    @UserLoginToken
    @PostMapping("/addressPage")
    @ApiOperation(value = "用户地址",notes = "用户地址")
    public ResponseData addressPage(@RequestBody PageBasic page) {
        return this.addressService.addressPage(getUserVo(), page);
    }

    @UserLoginToken
    @PostMapping("/addressDetail")
    @ApiOperation(value = "地址信息",notes = "地址信息")
    public ResponseData detail(@Valid @RequestBody Long id) {
        return ResponseData.success(this.addressService.getById(id));
    }

    @UserLoginToken
    @PostMapping("/insertAddress")
    @ApiOperation(value = "添加地址",notes = "添加地址")
    public ResponseData insertAddress(@Valid @RequestBody AddressInsertReq addressInsertReq) {
        return this.addressService.insertAddress(addressInsertReq, getUserVo());
    }

    @UserLoginToken
    @PostMapping("/updateAddress")
    @ApiOperation(value = "修改地址",notes = "修改地址")
    public ResponseData updateAddress(@Valid @RequestBody AddressUpdateReq addressUpdateReq) {
        return this.addressService.updateAddress(addressUpdateReq,getUserVo());
    }

    @UserLoginToken
    @PostMapping("/setDefaultAddress")
    @ApiOperation(value = "修改默认地址",notes = "修改默认地址")
    public ResponseData setDefault(@Valid @RequestBody Long id) {
        return this.addressService.setDefault(id);
    }

    @UserLoginToken
    @PostMapping("/deleteAddress")
    @ApiOperation(value = "删除地址",notes = "删除地址")
    public ResponseData deleteAddress(@RequestBody List<Long> ids) {
        return this.addressService.deleteAddressById(ids, getUserVo());
    }

    @PostMapping("/defaultAddress")
    @UserLoginToken
    @ApiOperation(value = "获取用户默认地址",notes = "获取用户默认地址")
    public ResponseData getDefaultAddress() {
        return this.addressService.getDefaultAddress(getUserVo());
    }

    public static void main(String[] args) {
        String chineseText = "你好世界";
        StringBuilder result = new StringBuilder();

        for (char c : chineseText.toCharArray()) {
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
            if (pinyinArray != null) {
                result.append(pinyinArray[0].charAt(0));
                break;
            } else {
                result.append(c);
                break;
            }
        }
        System.out.println(result.toString().toUpperCase());
    }
}

