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-other',
    templateUrl: './other.page.html',
    styleUrls: ['./other.page.scss'],
})
export class OtherPage implements OnInit {
    value = [false, false, false, false, false, false, false];
    yewei = 0;
    temp = 0;
    proc = 0;
    duration = 23;
    _inv;
    color;
    devices = [];
    fieldRegionNo = '';
    fieldRegionName = '';
    types = [];
    allBtns = [];

    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.getData(1);
    }

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

    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();
        }]);
    }

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

    initAllBtn() {
        if (this.types.length === this.devices.length && this.devices.length === 2) {
            let v = 0;
            if (this.devices.filter(a => a.value).length === this.devices.length) {
                v = 1;
            }
            this.allBtns = [{l: '全部', v: v, d: false, t: 0}];
        } else {
            const obj = [];
            this.types.map((a) => {
                const c = {l: a, v: 0, d: false, t: a === '泵' ? 1 : 2};
                if (this.devices.filter(b => b.fieldDeviceType === a && b.value).length ===
                    this.devices.filter(b => b.fieldDeviceType === a).length) {
                    c.v = 1;
                }
                obj.push(c);
            });
            this.allBtns = obj;
        }
    }

    allCheck(a) {
        a.d = true;
        const req = {
            fieldRegionNo: this.fieldRegionNo,
            op: a.v ? 0 : 1,
            type: a.t
        };
        this.load.toLoad('指令已发送, 等待处理结果', true);
        this.api.control.controlBatchSwitch([req, (data) => {
            if (data.code === 1) {
                this.getData(1);
                this.api.presentMsgToast(data.message);
            } else {
                this.api.presentMsgToast(data.message);
            }
            a.d = false;
            this.load.offLoad();
        }]);
    }

    getData(time) {
        if (time) {
            this.load.toLoad('加载中...', false);
        }
        this.api.app.getDeviceData([{fieldRegionNo: this.fieldRegionNo}, (data) => {
            if (data.constructor === Array) {
                this.devices = data;
                this.types = Array.from(new Set(data.map(a => a.fieldDeviceType)));
                if (time) {
                    this.initAllBtn();
                }
            } 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);
            }
        }]);
    }

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