<template>
    <heightResize ignore :append="['.page-option-block']" @resize="resize">
        <autoHeightTextarea v-model="current.input" :height="inputHeight" :placeholder="$t('hexString_input')"/>
        <option-block class="page-option-block">
            <FormItem>
                <ButtonGroup>
                    <Button type="primary" @click="codeGenString()">Convert</Button>
                    <!-- <Button type="primary" @click="handle('str')">Hex -> String</Button> -->
                </ButtonGroup>
            </FormItem>
            <FormItem>
                <Checkbox v-model="current.isUppercase">{{ $t('hexString_uppercase') }}</Checkbox>
            </FormItem>
        </option-block>
        <!-- <span>美化的内容&nbsp;&nbsp;&nbsp;&nbsp;：</span><input class="input_width_100" type="text" v-model="current.output_beautiful">
        <br>
        <span>字符串格式&nbsp;&nbsp;&nbsp;&nbsp;：</span><input class="input_width_100" type="text" v-model="current.output_str">
        <br>
        <span>字节数组格式：</span><input class="input_width_100" type="text" v-model="current.output_bytes"> -->
        <autoHeightTextarea :value="current.output" :height="outputHeight" :placeholder="$t('hexString_output')"/>
    </heightResize>
</template>
<script>
import heightResize from "../../tool/components/heightResize";
import autoHeightTextarea from "../../tool/components/autoHeightTextarea";

export default {
    components: {
        heightResize,
        autoHeightTextarea
    },
    created() {
        this.$initToolData('input')
    },
    methods: {
        codeGenString() {
            console.log('enter...')
            var ret = 'StringBuilder sb = new StringBuilder();\n'
            if (this.current.input) {
                var lst = []
                var max_length = 65500;
                var count = Math.floor(this.current.input.length / max_length);
                let index = 0;
                for (; index < count; index++) {
                    lst.push(
                        this.current.input.substr(index * max_length, max_length)
                    )
                }
                lst.push(
                    this.current.input.substring(index * max_length, this.current.input.length)
                )
                
                for (let index = 0; index < lst.length; index++) {
                    const element = lst[index];
                    ret += 'sb.append("' + element + '");\n';
                }
                ret += '\nString str = sb.toString();'
            }
            console.log('leave...', ret)
            this.current.output = ret
        },
        resize(height) {
            this.inputHeight = Math.min(320, Math.ceil(height / 2))
            this.outputHeight = height - this.inputHeight
            // this.outputHeight = 180
        }
    },
    data() {
        return {
            current: {
                input: "",
                isUppercase: false,
                output_beautiful: "",       // 一行，全字节内容，美化的内容
                output_str: "",             // 字符串格式
                output_bytes: "",           // 字节数组格式
                output: "",
                operation: ""
            },
            inputHeight: 100,
            outputHeight: 100
        }
    },
}
</script>

<style scoped>
.input_width_100 {
    width: 80%;
    margin: 8px 0;
}
</style>