import {Component, OnInit} from '@angular/core';
import {Transfer} from '../service/transfer';
import {Api} from '../service/api';
import {Load} from '../service/load';
import {ActivatedRoute} from '@angular/router';

@Component({
    selector: 'v-east',
    templateUrl: './east.page.html',
    styleUrls: ['./east.page.scss'],
})
export class EastPage implements OnInit {
    value = [];
    yewei = 0;
    temp = 0;
    proc = 0;
    duration = 23;
    nanClicked = false;
    beiClicked = false;
    nanLoading = false;
    beiLoading = false;
    _inv;
    color;
    nb = [];
    devices = [];
    fieldRegionNo = '';
    fieldRegionName = '';

    state = {
        refreshState: {
            currentState: 'deactivate',
            drag: false
        },
        direction: '',
        endReachedRefresh: false,
        data: [],
        directionName: 'up'
    };

    constructor(private tf: Transfer, private api: Api, private load: Load, private ac: ActivatedRoute) {
    }

    ngOnInit() {
        this.ac.queryParams.subscribe((data) => {
            this.fieldRegionNo = data.no;
            this.fieldRegionName = data.name;
        });
    }

    // ionic的生命周期函数
    ionViewWillEnter() {
        this.nanClicked = false;
        this.beiClicked = false;
        this.nanLoading = false;
        this.beiLoading = false;
        this.getData(1);
    }

    ionViewWillLeave() {
        clearTimeout(this._inv);
    }

    toggleNb(index) {
        if (!(this.beiLoading || this.nanLoading || this.value[0] || this.value[1])) {
            if (index === 0) {
                this.nanClicked = true;
                this.nanLoading = true;
            } else if (index === 1) {
                this.beiClicked = true;
                this.beiLoading = true;
            }
        } else if (this.value[index]) {
            this.confirm(this.nb[index]);
        }
    }

    toggle(a) {
        a.loading = true;
        const req = {
            fieldDeviceNo: a.fieldDeviceNo,
            op: a.value ? 0 : 1,
            ts: new Date().getTime()
        };
        this.load.toLoad('指令已发送, 等待处理结果', true);
        this.api.control.controlSwitch([req, (data) => {
            if (data && data.code === 1) {
                this.getData(1);
                this.api.presentMsgToast(data.message);
            } else {
                this.api.presentMsgToast(data.message);
            }
            a.loading = false;
            this.load.offLoad();
        }]);
    }

    cancel() {
        this.nanClicked = false;
        this.beiClicked = false;
        this.nanLoading = false;
        this.beiLoading = false;
    }

    confirm(a) {
        const req = {
            fieldDeviceNo: a.fieldDeviceNo,
            op: a.value ? 0 : 1,
            duration: this.duration,
            startTime: parseInt((new Date().getTime() / 1000).toString(), 10),
            ts: parseInt((new Date().getTime() / 1000).toString(), 10)
        };
        this.load.toLoad('指令已发送, 等待处理结果', true);
        this.api.control.controlSwitch([req, (data) => {
            if (data && data.code === 1) {
                this.getData(1);
                this.api.presentMsgToast(data.message);
            } else {
                this.api.presentMsgToast(data.message);
            }
            this.cancel();
            this.load.offLoad();
        }]);
    }

    onLeftClick() {
        this.tf.transfer({url: '/home'});
    }

    getData(time) {
        if (time) {
            this.load.toLoad('加载中...', false);
        }
        this.api.app.getDeviceData([{fieldRegionNo: this.fieldRegionNo}, (data) => {
            if (data.constructor === Array) {
                this.devices = data;
            } else {
                this.api.presentMsgToast(data.message || '获取数据失败');
            }
            this.load.offLoad();
        }]);

        this.api.app.getLiquidLevelData([{fieldRegionNo: this.fieldRegionNo}, (data) => {
            if (data.liquidLevel) {
                this.yewei = Number(data.liquidLevel);
                this.proc = (Number(data.liquidLevel) / (Number(data.liquidLevelMax) * 1.5)) * 100;
                this.color = Number(data.liquidLevel) > Number(data.liquidLevelMax) ||
                Number(data.liquidLevel) < Number(data.liquidLevelMin) ? 'red' : '';
            }
            if (data.temp) {
                this.temp = Number(data.temp);
            }
        }]);

        this.api.app.getNorthSouthWaterData([{fieldRegionNo: this.fieldRegionNo}, (data) => {
            if (data.constructor === Array) {
                this.nb = data;
                this.value = this.nb.map(a => a.value);
            } else {
                this.api.presentMsgToast(data.message || '获取数据失败');
            }
        }]);
    }

    pullToRefresh(event) {
        if (event === 'down') {
            this.getData(1);
        }
    }
}
