// 定义货币符号类型枚举
export enum CurrencySymbolType {
  None,
  AsSystem,
  UserSetting
}

// 定义控制辅助类
class ControlHelper {
  static ShowTip(element: any, message: string) {
    console.log(message);
  }
}

export class CurrencyBase {
  private _currentIndex: number = 0;
  private _oldText: string = "";
  private _firstInputZero: boolean = true;
  private _isInputDot: boolean = false;
  private _isDelete: boolean = false;
  private _formatType: string = "";
  private _txtValue: string = "";
  private _initTxtLength: number = 0;

  private _decimalAccuracy: number = 0;
  private _minValue: number = 0;
  private _maxValue: number = 9999999999999999.00000;
  private _accuracy: number = 2;

  private _text: string = "";
  private _caretIndex: number = 0;

  get Val() {
    return this._text.replace(/,/g, "");
  }

  get Text() {
    return this._text;
  }

  set Text(value: string) {
    this._text = value;
  }

  get CaretIndex() {
    return this._caretIndex;
  }

  set CaretIndex(value: number) {
    this._caretIndex = value;
  }

  get MinValue() {
    return this._minValue;
  }

  set MinValue(value: number) {
    this._minValue = value;
  }

  get MaxValue() {
    return this._maxValue;
  }

  set MaxValue(value: number) {
    this._maxValue = value;
  }

  get Accuracy() {
    return this._accuracy;
  }

  set Accuracy(value: number) {
    this._accuracy = value;
  }

  // 模拟获取焦点事件
  OnGotFocus() {
    if (this._text.trim() !== "" && parseFloat(this._text.trim()) === 0) {
      this._text = "";
      this._oldText = this._txtValue;
    }
    // 模拟全选
    this.CaretIndex = this._text.length;
  }

  // 模拟失去焦点事件
  OnLostFocus() {
    this._firstInputZero = true;
    this._isInputDot = false;
    if (this._text.trim() !== "") {
      if (this._text[0] === "-") {
        let value = parseFloat(this._text.substring(1));
        this._text = "-" + this.FormatType(this._formatType, value);
      } else {
        let value = parseFloat(this._text);
        this._text = this.FormatType(this._formatType, value);
      }
    }
    if (this._text === "") {
      this._text = this._txtValue;
    }
  }

  // 模拟键盘按下事件
  OnPreviewKeyDown(key: string) {
    if (key.includes("Control") || key.includes("Shift") || key.includes("Alt")) {
      return;
    }
    this._isInputDot = false;
    this._isDelete = false;

    if (key === ".") {
      this._isInputDot = true;
      this.CaretIndex = this._text.indexOf(".") + 1;
      this._currentIndex = this.CaretIndex;
      return;
    } else if (key === "Backspace") {
      this._isDelete = true;
      if (this._text !== "" && this.CaretIndex !== 0) {
        if (this._text[this.CaretIndex - 1] === "," || this._text[this.CaretIndex - 1] === ".") {
          this.CaretIndex--;
          this._currentIndex = this.CaretIndex - 1;
        } else {
          this._currentIndex = this.CaretIndex - 1;
        }
      }
      this._firstInputZero = false;
    } else if (key === "Delete") {
      this._isDelete = true;
      if (this._text !== "" && this.CaretIndex !== this._text.length) {
        if (this._text[this.CaretIndex] === "," || this._text[this.CaretIndex] === ".") {
          this.CaretIndex++;
          this._currentIndex = this.CaretIndex;
        } else {
          this._currentIndex = this.CaretIndex;
        }
      }
      this._firstInputZero = false;
    } else if (key === "-") {
      if (this._text.indexOf("-") >= 0 || this.CaretIndex !== 0) {
        this._currentIndex = this.CaretIndex;
        return;
      } else {
        this._currentIndex = this.CaretIndex + 1;
      }
    } else if (!this.IsDigitalKey(key)) {
      this._currentIndex = this.CaretIndex;
      return;
    } else {
      if (this._text.length === 0 || this._text === "-") {
        this._firstInputZero = true;
      } else {
        this._firstInputZero = false;
      }
      this._currentIndex = this.CaretIndex + 1;
    }

    if (this._text.trim() !== "") {
      let olddec = parseFloat(this._text);
      this._oldText = this.FormatType(this._formatType, olddec);
    }
    let doubleText = parseFloat(this._oldText);
    if (doubleText < this._minValue || doubleText > this._maxValue) {
      return;
    }
  }

  // 模拟文本更改事件
  OnTextChanged() {
    if (this._text.trim() !== "") {
      let text = "0";
      let value = 0;
      if (this._minValue < 0 && this._text[0] === "-") {
        if (this._text === "-") {
          this.CaretIndex = this._currentIndex;
          return;
        }
        value = parseFloat("0" + this._text.substring(1));
        text = "-" + this.FormatType(this._formatType, value);
      } else {
        value = parseFloat(this._text);
        text = this.FormatType(this._formatType, value);
      }

      if (parseFloat(text) < this._minValue) {
        this._text = "0.00";
        ControlHelper.ShowTip(this, "最小值为" + this._minValue);
        return;
      }
      if (parseFloat(text) > this._maxValue) {
        this._text = "0.00";
        ControlHelper.ShowTip(this, "最大值为" + this._maxValue);
        return;
      }

      this._text = text;

      if (parseFloat(text) >= this._minValue && parseFloat(text) <= this._maxValue) {
        if (value === 0) {
          if (this._firstInputZero) {
            if (this._text[0] === "-") {
              this._text = "-" + this._txtValue;
            } else {
              this._text = this._txtValue;
            }
            let n = this._text.indexOf(".");
            this.CaretIndex = n !== -1 ? n : this.CaretIndex;
          } else if (this._isDelete) {
            this.CaretIndex = 1;
          } else {
            if (!this._isInputDot && this._text.indexOf(".") >= 0 && this.CaretIndex > this._text.indexOf(".")) {
              if (this.CaretIndex === this._text.indexOf(".") + 1) {
                this.CaretIndex = this._text.indexOf(".");
                return;
              }
            }
            this.CaretIndex = this._currentIndex;
          }
          return;
        }
        if (this._oldText) {
          if (this._oldText.length - this._text.length === 2 && value !== 0) {
            if (this._currentIndex > 1) {
              this.CaretIndex = this._currentIndex - 1;
            } else if (this._currentIndex === 1) {
              this.CaretIndex = 1;
            } else if (this._currentIndex === 0) {
              this.CaretIndex = 0;
            }
          } else if (this._text.length - this._oldText.length === 2 && !this._isDelete) {
            this.CaretIndex = this._currentIndex + 1;
          } else {
            this.CaretIndex = this._currentIndex;
            if (this._currentIndex < this._text.indexOf(".")) {
              if (this._text[0] === "-" && !this._isDelete) {
                this._currentIndex = this._text.indexOf(".") + 1;
                this.CaretIndex = this._currentIndex;
              }
            }
            if (this._text.indexOf(".") + 1 === this.CaretIndex && !this._isInputDot) {
              let n = this._text.indexOf(".");
              this.CaretIndex = n !== -1 ? n : this._currentIndex + 1;
            }
          }
        } else {
          this.CaretIndex = this._text.length;
        }
      }
    } else {
      this._text = "0.00";
      this.CaretIndex = 1;
    }
  }

  // 判断是否为数字键
  private IsDigitalKey(key: string) {
    return /^\d$/.test(key);
  }

  // 格式化数值
  private FormatType(formatType: string, value: number) {
    if (value.toString().indexOf(".") > -1) {
      let s = value.toString().split(".");
      value = parseFloat(s[0] + "." + s[1].substring(0, this._accuracy));
    }
    return value.toFixed(this._accuracy);
  }
}