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

@Component({
    selector: 'v-env-detail',
    templateUrl: './env-detail.page.html',
    styleUrls: ['./env-detail.page.scss'],
})
export class EnvDetailPage implements OnInit {

    constructor(public nav: NavController,
                private api: Api,
                private ac: ActivatedRoute,
                private load: Load,
                private tf: Transfer) {
        this.ac.queryParams.subscribe((data) => {
            if (data.no) {
                this.fieldRegionNo = data.no;
            }
        });
    }

    item = {
        factoryNo: '',
        factoryName: '',
        fieldRegionNo: '',
        fieldRegionName: '',
        temp: 0,
        humi: 0,
        heatStressLevel: 0,
        heatStressColor: '#61C7CC',
        heatStressLevelName: '无',
        online: 0,
        operationModel: 1,
        isWisdomModel:  1,
        alarm: 1,
        inHouse: {
            inHouse: 0,
            inHouseState: false,
            inHouseName: '离舍',
            fieldDeviceNo: '',
            fieldDeviceType: '',
        },
        ts: 0
    };
    inHouseName = '离舍';
    inHouseState = false;

    fieldRegionNo = '';
    loading = false;

    // 发送指令延时判断
    comm = {
        remain: 20,
        isLoading: true
    };

    fanLink = '';
    showerLink = '';
    lightLink = '';
    waterchannelLink = '';

    ngOnInit() {
        this.init();
    }

    // ionic的生命周期函数
    ionViewWillEnter() {
        console.log('ionViewWillEnter');
        this.init();
    }

    init() {
        this.comm.isLoading = false;
        this.comm.remain = 0;
        this.ac.queryParams.subscribe((data) => {
            if (data.no) {
                this.fieldRegionNo = data.no;
                this.getData(0);
            }
        });
    }

    getData(time) {
        if (time) {
            this.load.toLoad('加载中...', false);
        }
        this.api.cowshedApp.getRealDataByFieldRegionNo([{fieldRegionNo: this.fieldRegionNo}, (data) => {
            if (data) {
                this.item = data;
                this.item.heatStressLevelName = this.getHeatStressLevelName(data.heatStressLevel);
                this.item.inHouse.inHouseName = this.getInHouseState(data.inHouse.inHouse);
                this.item.inHouse.inHouseState = data.inHouse.inHouse === 1 ? true : false;
                this.inHouseName = this.getInHouseState(data.inHouse.inHouse);
                this.inHouseState = data.inHouse.inHouse === 1 ? true : false;
                this.initLayout();
            }
        }]);
    }

    initLayout() {
        // this.showerLink = '/env-detail/shower/' + this.fieldRegionNo;
        // this.lightLink = '/env-detail/light/' + this.fieldRegionNo;
        // this.waterchannelLink = '/env-detail/waterchannel/' + this.fieldRegionNo;
        this.fanLink = '/env-detail/fan/';
        this.showerLink = '/env-detail/shower/' + this.fieldRegionNo;
        this.lightLink = '/env-detail/light/' + this.fieldRegionNo;
        this.waterchannelLink = '/env-detail/waterchannel/' + this.fieldRegionNo;

        if (this.item) {
            this.item.heatStressColor = this.item.heatStressLevel === 0 ? '#13AE67' :
                this.item.heatStressLevel === 1 ? '#61C7CC' :
                    this.item.heatStressLevel === 2 ? '#F8B72D' :
                        this.item.heatStressLevel === 3 ? '#F2401C' : '#F2401C';
        }
    }

    getHeatStressLevelName(level) {
        let levelName = '';
        switch (level) {
            case 0:
                levelName = '无';
                break;
            case 1 :
                levelName = '轻';
                break;
            case 2 :
                levelName = '中';
                break;
            case 3 :
                levelName = '重';
                break;
            case 4 :
                levelName = 'II重';
                break;
            default:
                break;
        }
        return levelName;
    }

    getInHouseState(state) {
        let stateName = '离舍';
        switch (state) {
            case 0:
                stateName = '离舍';
                break;
            case 1:
                stateName = '在舍';
                break;
            default:
                stateName = '';
                break;
        }
        return stateName;
    }

    // 在离舍开关控制
    inHouseControl(inHouseState) {
        if (this.item.inHouse.fieldDeviceNo !== null && this.item.inHouse.fieldDeviceNo !== '') {
            const opt = {fieldDeviceNo: '', fieldDeviceType: '', op: 0, ts: 0};
            opt.fieldDeviceNo = this.item.inHouse.fieldDeviceNo;
            opt.fieldDeviceType = this.item.inHouse.fieldDeviceType;
            opt.op = inHouseState ? 1 : 0;
            opt.ts = new Date().valueOf();

            this.comm.remain = 20;
            this.api.control.controlCowedSwitch([opt, (data) => {
                // if (data && data.code === 1) {
                //     this.getData(1);
                //     this.api.presentMsgToast(data.message);
                //     // this.alarmMsg.locked = false;
                // } else {
                //     this.api.presentMsgToast(data.message);
                // }
                this.getData(0);
                this.api.presentMsgToast(data.message);
                this.comm.remain = 0;
            }]);
        }
    }

    toHistory(item) {
        const query = 'env-history?no=' + item.fieldRegionNo + '&name=' + item.fieldRegionName + '&heatStressLevel=' + item.heatStressLevel;
        this.nav.navigateForward(query);
    }
    // doRefresh(event) {
    //     setTimeout(() => {
    //         this.getData(0);
    //         event.target.complete();
    //     }, 2000);
    // }
}
