<template>
  <li :class="'form-radio ' + sizeClass">
    <input type="radio" :id="nextId" :name="itemName" :checked="checkedState" :disabled="disabledState" v-on:click.stop.prevent="clickMe">
    <label :class="fontSizeClass" v-on:click.stop.prevent="clickMe">{{ labelState }}</label>
  </li>
</template>
<script>
import eventBus from './bus';

const SIZE_LARGE = 'large';
const SIZE_SMALL = 'small';
const SIZE_NORMAL = 'normal';
const CSS_LARGE = 'form-radio--large';
const CSS_SMALL = 'form-radio--small';
const CSS_FONT_LARGE = 'text--large';
const CSS_FONT_MEDIUM = 'text--med';
const CSS_FONT_SMALL = 'text--small';

var seed = 0;

// 单个单选框，仅作为BoxGroup的子组件使用!
export default {
  name: 'taurus-radiobox',
  created: function () {
    this.checkedState = this.checked;
    this.labelState = this.label;
    this.disabledState = this.disabled;
    this.valueState = this.value;
    eventBus.$on('toggle-check-state', this._toggleCheckState);
    eventBus.$on('toggle-usability', this._toggleUsability);
  },
  beforeDestroy: function () {
    eventBus.$off('toggle-check-state');
    eventBus.$off('toggle-usability');
  },
  data: function () {
    return {
      size: SIZE_NORMAL,
      itemName: '',
      checkedState: false,
      labelState: '',
      disabledState: false,
      valueState: ''
    };
  },
  computed: {
    sizeClass: function () {
      if (this.size === SIZE_LARGE) {
        return CSS_LARGE;
      } else if (this.size === SIZE_SMALL) {
        return CSS_SMALL;
      } else {
        return '';
      }
    },
    fontSizeClass: function () {
      if (this.size === SIZE_LARGE) {
        return CSS_FONT_LARGE;
      } else if (this.size === SIZE_SMALL) {
        return CSS_FONT_SMALL;
      } else {
        return CSS_FONT_MEDIUM;
      }
    },
    nextId: function () {
      return 'tau-chk' + seed++;
    }
  },
  props: {
    // 此属性只读!
    idx: {
      default: 0,
      type: Number
    },
    checked: Boolean,
    label: String,
    disabled: Boolean,
    value: String
  },
  methods: {
    _toggleCheckState: function (source, item, checked) {
      if (source === this.$parent) {
        if (item === this) {
          if (this.checkedState === false) {
            this.checkedState = true;
            this._fireEvents();
          }
        } else {
          if (this.checkedState === true) {
            this.checkedState = false;
            this._fireEvents();
          }
        }
      }
    },
    _toggleUsability: function (source, item, disabled) {
      if (source === this.$parent && item === this) {
        this.disabledState = disabled;
      }
    },
    clickMe: function () {
      eventBus.$emit('radio-item-check', this, this.idx);
    },
    enable: function () {
      if (this.disabledState === true) {
        this.disabledState = false;
      }
    },
    disable: function () {
      if (this.disabledState === false) {
        this.disabledState = true;
      }
    },
    _fireEvents: function () {
      eventBus.$emit('item-changed', this, this.idx, this.checkedState);
      eventBus.$emit(this.checkedState ? 'item-checked' : 'item-unchecked', this, this.idx);
    }
  }
};
</script>
