import { _decorator, EditBox, Label, Node, Sprite } from 'cc';
import { base_ui } from '../fw/base_ui';
import { userDataManager } from '../manager/userDataManager';
import { tools } from '../tools';
import { GameManager } from '../GameManager';
import { SdkUtil } from '../sdkUtil';
import { http } from '../http';
import { config } from '../config';
import { uiManager } from '../manager/uiManager';
import { ClientEvent } from '../lib/clientEvent';
const { ccclass, property } = _decorator;

@ccclass('edit_user_info')
export class edit_user_info extends base_ui {
    @property(Node) btn_back:Node = null
    @property(Node) img_avatar:Node = null
    @property(Node) img_avatar_border:Node = null
    @property(Node) btn_edit_avatar:Node = null
    @property(EditBox) editbox_name:EditBox = null
    @property(Node) btn_edit_name:Node = null
    @property(Node) node_region:Node = null
    @property(Node) lab_region:Node = null 
    @property(Node) btn_save:Node = null

    private avatar_url:string = ''

    start() {
        this.onButtonListen(this.btn_back, ()=>{
            this.close()
        })
        this.onButtonListen(this.btn_edit_avatar, ()=>{
            this.onClickEditAvatar()
        })
        this.onButtonListen(this.btn_edit_name, ()=>{
            if(this.editbox_name.isFocused()==false) {
                this.editbox_name.setFocus()
            }
        })
        this.onButtonListen(this.node_region, ()=>{
            GameManager.showChangeArea()
        })
        this.onButtonListen(this.btn_save, ()=>{
            this.requestSaveUserInfo()
        })
        this.initData()
    }

    private initData() {
        let user_data = userDataManager.user_data

        this.avatar_url = user_data.avatarUrl
        tools.loadRemoteImg(user_data.avatarUrl, (r)=>{
            this.img_avatar.getComponent(Sprite).spriteFrame = r.sf
        })
        if(user_data.photo_frame_img && user_data.photo_frame_img.length>0) {
            tools.loadRemoteImg(user_data.photo_frame_img, (r)=>{
                this.img_avatar_border.getComponent(Sprite).spriteFrame = r.sf
            })
        }
        this.editbox_name.string = user_data.nickName
        this.lab_region.getComponent(Label).string = user_data.province_name + ' - ' + user_data.city_name
        if(tools.sys_config.k_region==0) {
            this.node_region.active = false
        }
    }

    private onClickEditAvatar() {
        SdkUtil.choosSystemImage((filePath)=>{
            let fromdata = {'stype': 2}
            uiManager.Instance().showLoading()
            http.uploadFile(config.API.upload_img, filePath, fromdata, null,(err,d)=>{
                uiManager.Instance().hideLoading()
                if(!err){
                    let data = JSON.parse(d)
                    if(data.code===config.status.SUCCESS){
                        // console.log('data=',data.content)
                        this.avatar_url = data.content.surl
                        tools.loadRemoteImg(this.avatar_url, (res)=>{
                            this.img_avatar.getComponent(Sprite).spriteFrame = res.sf
                        })
                    }
                }
            })           
        })
    }

    private requestSaveUserInfo() {
        if(this.editbox_name.string.length<=0) {
            uiManager.showToast('请填写昵称')
            return
        }

        let is_change:boolean = false
        let user_data = userDataManager.user_data
        if(user_data.avatarUrl!=this.avatar_url) {
            is_change = true
        }
        if(user_data.nickName!=this.editbox_name.string) {
            is_change = true
        }

        if(is_change) {
            let opt = {'avatarUrl':this.avatar_url,'nickName':this.editbox_name.string}
            uiManager.Instance().showLoading()
            http.post(config.API.up_user_info,opt,(err,d)=>{
                uiManager.Instance().hideLoading()
                if(!err){
                    let nd = JSON.parse(d)
                    if(nd.code === config.status.SUCCESS){
                        // console.log("修改用户信息", nd.content)
                        uiManager.showToast('用户信息修改成功')
                        let content = nd.content
                        userDataManager.user_data.avatarUrl = content.avatarUrl
                        userDataManager.user_data.nickName = content.nickName
                        ClientEvent.dispatchEvent(config.UI_EVENT.UPDATE_USER_INFO)
                    }
                }
            })
        } else {
            this.close()
        }
    }
}

