/// <reference path='../tsd.d.ts' />
/// <reference path='../../proxy/execImap.ts' />

interface ICallBack {
   <T> ( err?: Error, result?: any, data?: any  ) : void;
}

declare module "shortid" {
    interface main {
        generate:()=>string;
    }
    let u:main
    export = u
}

interface VE_IPptpStream {
    type?: string;
    buffer: Buffer;
    host: string;
    port: number;
    cmd: number;
    ATYP: number;
    uuid?: string;
    length?:number;
    randomBuffer?: Buffer
}

interface IpairConnect  {
	listenFolder: string;
	saveFolder: string;
}

interface IinputData {
    account:string;
    email:string;
    password:string;
    smtpServer:string;
    imapServer:string;
    smtpUserName:string;
    imapUserName:string;
    smtpUserPassword:string;
    imapUserPassword:string;
    smtpPortNumber:string;
    imapPortNumber:string;
    imapSsl:boolean,
    smtpSsl:boolean,
    //  imap 0 == deable, 1 == imap connectReady, 2 == imapConnect between VpnVia.email and client 
    Status:number;
    publicKey:string;
    uuid:string;
    remoteImapConnect?:boolean;
	systemPassword: string;
	publicKey_id: string;
	serverMailBoxName: string;
    imapConnect?:boolean;
    dummyData:string;
    VpnviaEmailCheckOK: boolean;
    power: string;
    machineID: string;
    runningContainerData: string

}
interface INodeMasterToContainerFirstTimeData {
    sendData: string; 
    keyPass: string; 
    keyPassFileName: string;
    keyID: string;
    privateKeyAsc: string;
    pubkeyAsc: string
}
interface INodeClientSocket {
    serverMailBoxName: string;
    command: string,
    data: string;
    uid: string;
    account: string;
}
interface IContainer_running {
    domain?: string;
    key_id?: string;
    running_pve_region: string;
    account: string;        //      user aaccount
    command?: string; 
    running_docker_name: string;
    is_test_connect?: boolean;
    create_date_uuid?: string;
    imap_user_name?: string;
    attachments?: string;
    power?: string
    create_date?: string;
    pid?: string;
    user_addon?: string;
    start_date?: string;
    running_cluster_name?: string;
    alife?: boolean;

    //PRIMARY KEY ((pve_region), name, command, create_date_uuid)) WITH CLUSTERING ORDER BY ( name DESC, command DESC, create_date_uuid DESC);';

}
interface IcheckAccountReturn {
	data: IinputData;
	dataStr: string;
	sentMessage: ISentMessage;
	sentMessageEncrypt: string;
	serverMailFolder: string;
	AlreadySentMail: boolean;
	container: VE_IContainer;
}
interface ICpuPowerCluster {
    socketManager?: string;
    socketmanagerData?: IContainer_running;
    command: string;    //  exec command
    keepRun?: boolean;
    params: any;        //  exec command params
    CallBack?: ICallBack;
    power?: number;
    ID?: string;
    err?: Error
}
interface IshellDecryptionReturnData {
	text: string;
	sign: boolean[];
}
interface VE_IDocker {
	keyPublic: string;
	keyPrivate: string;
	keyPassword: string;
    keyID: string;
	name: string;
	veRegion: string;
	createDate?: Date;
    cluster : VE_IDockerCluster
    clusterName : string;
}

interface VE_IDockerCluster {
    cluster_name: string;
    clusterID: string;
    clusterCreateDate?: Date;
    publicIp4: string;
}

interface IFirstRun {
    private_ip4: string;
    public_ip4: string;
    cluster_name: string;
    provider: string;
    pve_region: string;
    id: string;
    create_date: Date;
    private_region: string;
    destroy?: Date;
    keepTime?: number;
}
interface IClientEmailCommand {
    command: string;
    data: string;
    account: string;
    result: any;
}

interface INewKeyPair {
	email:string;
	name: string;
	keyBitLength: string;
	password: string;
}
interface ICluster {
    name: string;
    create_date: Date,

}

interface VE_ISocket_dockerCommandRequest_emit {
	dockerName: string;
	requestID: string;
}

interface VE_ISocket_dockerCommandRequest {
	dockerName: string;
	message: any;
	requestID: string;
}

interface ICommand {
	command: string;
	env: Object;
} 
interface ITimeCount {
	year: number,
	monthly: number,
	day: number,
	hour: number,
	tenMins: number,
	min: number
}
interface gcloud_dataStore_dataExclude {
	name: string;
	value: any;
	excludeFromIndexes: boolean;
}
interface master_init_json_initCommand {
	exec: string;
	args: string[];
	loop: number
}
interface master_init_json {
	initCommand: master_init_json_initCommand[]
}
interface IPve_Global_Production {
	name: string;
	name_language: string[];
	create_date: string;
	day_max_transfer: number;
	can_over_transfer: boolean;
	over_transfer_fee: string;
	active: boolean;	
	power_node: number;
	price: string;
	validity: number;
	is_promotions: boolean;
	limit_quantity: number;
	discount: number;
	annual: string;
	multi_email: number;
	detail: string[],
	bg_color: string;
	can_vpn: boolean;
	order: number;
	monthly_max_transfer: number;
}
interface IPve_global_pve_region {
	provider_name: string;
	show_text: string[];
	sub_script: string;
	pve_region: string;
	provider_region: string[];
	available: boolean
}

interface IPve_global_account {
    account: string;                               //          account email
    publicKey: string;                             //          currently public pem
    publicKeyID: string;
    keyDestroyPassword: string;   
    machineID: string;
    createDate: Date;                             //          account regiest time
	cardAccount: string;
    production: string;                             //		    当前所购产品
    activeStatus: boolean;                          //		    用户激活狀態
    balanceTransferStartDate: Date;               
	tempPublicKey: string;
    tempPublicKeyID: string;
    
            
    // PRIMARY KEY ((prepare), email)) WITH CLUSTERING ORDER BY (email DESC );';
}
interface VE_ISaveLog_nodeLog {
    nodeName: string;
    dockerName: string;
    containerName: string;
    log: string;
    level: number;

}
interface VE_IPublicKey {
    publicKey: string;
    publicKeyID: string
}
interface VE_ITempPublicKey extends VE_IPublicKey {
    createDate: Date;
    keyDestroyPassword: string;
}
interface VE_IAccountProduction {
    createDate: Date;
    production: string;
    maxBalanceTransfer: number
}
interface VE_IPayment {
    
}

interface VE_IContainer {
    name: string
    createDate: Date;
    power: number,
    running: gcloud_datastore_keyOption,
    runningDockerName: string;
    account: string,

}
interface VE_IContainerRunningData {
    docker: VE_IDocker,
    transfer: number
    startDate: Date,
    endDate: Date
    keepTime: number,
    account: string
}
interface VE_IAccount {
    publicKey: VE_IPublicKey,
    tempPublicKey: VE_ITempPublicKey,
    machineID: string,
    createDate: Date,
    activeStatus: boolean,
    account: string,
    production: VE_IAccountProduction,
    activeDate: Date,
    payment: VE_IPayment
}
interface ISentMessage {

//		if client's public key is different than regiest will true
		publicKeyChanged: boolean;
		serverError: string;
		//		system message for user
		message: string[];		
		keyDestroyPassword: string;
		//		active user	
		active: boolean;
		serverMailFolder: string;
		tempText: string;
		toServerPassword: string;
		toServer_command: String;
		account: string;
		publicKeyId: string;
		production: string;
		accountTypeList: IAccountType[];
        accountType: string;
		stripePayment: stripePayment;
		random: string;
		region: region[];
        echo: boolean;
        disconnect: boolean;
        haveDockerPublicKey: boolean;
}
interface region {
	
}
interface stripePayment {
	stripeToken: string;
	stripeTokenType: string;
	stripeEmail: string;
}
interface IContainer {
	isTestConnect: boolean;
	alife: boolean;
	createDate: Date;
	power: string
}
interface IAccountType {
    name: string;
    detail: string;
    price: string;
    active: boolean;
    is_promotions: boolean;
    annual: string;
    monthly_max_transfer: string;
    multi_email: string;
    over_transfer_fee: string;
    day_max_transfer: string;
}
interface VE_IgreData {
    clientIp: string;
    data: Buffer;
    
}

interface ISaveDataPool {
	buffer: Buffer;
	uuid: string;
	index: number;
	noExcrypt: boolean;
}

declare module Pem {
    interface option {
        serviceKey?: any;
        serviceKeyPassword?: string;
        serviceCertificate?: any;
        serial?: number;
        selfSigned?: boolean;
        csr?: any;
        days: number;
        extFile?: any;
        config?: any;
    }
    interface key {
        certificate: string;
        csr: string;
        serviceKey: string;
        clientKey: string;
    }
    export interface Static {
        createCertificate: ( option: option, CallBack: ( err: Error, keys: key ) => void ) => void
    }
}
declare module "pem" {
    var pem: Pem.Static;
    export = pem;
}


declare module "request-image-size" {
    interface length { 
        height: number
        width: number
        type: string
    }
    interface pen {
        ( option: any , CallBack: ( err: Error, dimensions, length: length ) => void ): void
    }
    var pem: pen
    export = pem;
}
interface domainRecord {
	ipv4: string;
	ipv6: string;
}
declare module "dns" {
    interface lookup_option {
        family?: number;
        hints?: number;
        all?: boolean
    }
    interface address {
        address: string;
        family: number
        connect?: number []
    }
    export function lookup ( domain: string, option: lookup_option , callback: ( err: Error, address: address[] ) => void ): string;

}
interface AsyncBooleanIterator1 <T> {( item: T, callback: ( err?: Error, truthValue?: boolean ) => void ): void; }
interface Async {
    
    someSeries < T > ( arr: T[], iterator: AsyncBooleanIterator1 < T >, callback?: ( err?: Error, result?: boolean ) => void ) : any
}
declare module "crypto" {
    interface Cipher {
        getAuthTag: () => Buffer;
    }
    interface Decipher {
        setAuthTag: ( tag: Buffer ) => void;
    }
}

declare module "net" {
    import * as events from "events";
    export interface Server extends events.EventEmitter {
        getConnections: ( Callback: ( err?: Error, num?: number ) => void ) => void
    }
}

declare module "http" {
    import * as events from "events";
    import * as stream from "stream"
    export interface ServerResponse extends events.EventEmitter, stream.Writable {
        finished: boolean;
    }
}