import QtQuick 2.0
import Sailfish.Silica 1.0
import "../JSONListModel"

ListItem {
    id: showDelegate
    width: ListView.view.width
    contentHeight: showImg.height + showName.height + (Theme.paddingLarge * 4)

    property bool favorite: getFav(model.id)
    property string prev: ""
    property string next: ""
    property string nextse: ""
    property string genretext: ""

    ListModel{id: sgenres}

    function getArrary(ent, model) {
        if (ent) {
            console.log("LEN: "+ent.count)
            for (var i = 0; i < ent.count; i ++) {
                console.log(ent.get(i))
                model.append(ent.get(i))
            }
        }
    }

    JSONSimple {
        id: nextshow
        onReadyChanged: {
            if (ready) {
                simple.airdate ? next = simple.airdate : next = "?"
                next !== "" ? nextse = "S"+simple.season+"E"+simple.number : nextse = ""
            }
        }
    }

    JSONSimple {
        id: prevshow
        onReadyChanged: {
            if (ready) {
                simple.airdate ? prev = simple.airdate : prev = "?"
                if (model._links.nextepisode)  {
                    nextshow.source = model._links.nextepisode.href
                } else {
                    next = "?"
                    nextse = ""
                }
            }
        }
    }

    RemorseItem {
        id: remorse
         wrapMode: Text.WordWrap
    }

    function showRemorseItem() {
        var idx = index
        remorseAction( "Removing "+name+" from favorites!", function() {delFav(model.id);favorite = false})
    }

    OpacityRampEffect {
        sourceItem: showRect
        direction: OpacityRamp.TopToBottom
        offset: 0.0
        slope: 1.0
    }

    Rectangle {
         id: showRect
         anchors.left: parent.left
         anchors.right: parent.right
         width: parent.width
         height: showName.height + Theme.paddingLarge
         color: Theme.rgba(Theme.highlightBackgroundColor, Theme.highlightBackgroundOpacity)
    }

    Image {
       id: nextIcon
       anchors.verticalCenter: showRect.verticalCenter
       anchors.right: showRect.right
       anchors.rightMargin: Theme.paddingMedium
       source: "image://theme/icon-m-right"
    }

    Image {
       id: seenIcon
       visible: false
       anchors.right: parent.right
       anchors.bottom: showImg.bottom
       anchors.rightMargin: Theme.paddingMedium
       source: "image://theme/icon-m-acknowledge"
    }

    Text {
         id: showName
         text: model.name !== null ? model.name : ""
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         anchors.leftMargin: Theme.paddingMedium
         anchors.rightMargin: Theme.paddingMedium
         anchors.left: parent.left
         anchors.right: nextIcon.left
         anchors.verticalCenter: showRect.verticalCenter
         wrapMode: Text.ElideRight
         font.pixelSize: Theme.fontSizeLarge
         maximumLineCount: 1
    }

    Component.onCompleted: {
     //   console.log("COMPLETED")
        //sgenres.clear()
   //     getArrary(model.genres,sgenres)
        //blahh = model.get(0).genres
        //console.log("GENRES: "+blahh.get(1))
        if (model._links.previousepisode) {
            prevshow.source = model._links.previousepisode.href
        } else {
            prev = "";
            next = "";
            nextse = ""
        }
        //sgenres = model.genres
        //console.log("GENRE: "+sgenres.get(0)[0])
        //var temp = [] //model.genres.count
        //temp = model.genres
        //var genretext = ""
        //console.log("LOG: "+ genres.get(1).modelData)
        //var genre = temp.split(",")
       // for(var i = 0; i < model.genres.count; i++) {
            //fruitModel.get(0).attributes.get(1).value
            //genretext = genretext + "|" + model.genres.get(i) //model.genres.sublist(i)


       // }
        //return genretext

    }

    Image {
        id: showImg
        anchors.top: showRect.bottom
        anchors.margins: Theme.paddingLarge
        anchors.left: parent.left
        fillMode: Image.PreserveAspectFit
        source: model.image ? model.image.medium : "../No-Image-.png"
        width: height * 0.8
//        height: (showName.height + showStatus.height + showPrem.height + showRating.height + showNext.height + showPrev.height + Theme.paddingMedium) * showImg.scale
        height: (showName.height + showS.height + showP.height + showR.height + showN.height + showPr.height + Theme.paddingLarge) * showImg.scale
        scale: 1.1

        BusyIndicator {
            id: imgBysy
            running: showImg.progress !== 1.0
            size: BusyIndicatorSize.Large
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
       }
    }

    Image {
        id: favIcon
        //visible: false
        anchors.right: parent.right
        anchors.top: showImg.top
        //anchors.verticalCenter: showStatus.verticalCenter
        height: showStatus.height + (showStatus.height/2)
        width: height
        anchors.rightMargin: Theme.paddingMedium
        source: favorite ? "image://theme/icon-m-favorite-selected" : "image://theme/icon-m-favorite"
        MouseArea {
            anchors.fill: favIcon
            onClicked: if (getFav(model.id)) {showRemorseItem()} else {
                           console.log("ID: "+model.id)
                           var network
                           model.webChannel ? network = model.webChannel.name  : network = model.network.name
                           addFav(model.id, model.name, showImg.source, model.summary, model.status, model.premiered, model.rating.average, network, model.updated, prev, next, nextse, model.externals.imdb);
                           reloadFav()
                           favorite=true
            }
        }
        BusyIndicator {
            id: addBusy
            running: false
            size: BusyIndicatorSize.Medium
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
        }
    }

    Text {
         id: showS
         text: "Status:"
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.WordWrap
         anchors.topMargin: Theme.paddingMedium
         anchors.bottom: showP.top
         anchors.left: showImg.right
         //anchors.right: parent.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
     }

    Text {
         id: showStatus
         text: model.status !== null ? model.status : ""
         //color: highlighted ? Theme.highlightColor : Theme.primaryColor
         color: {//highlighted ? Theme.highlightColor : running && f_next !== "?" ? Theme.primaryColor : ended ? Theme.errorColor : Theme.secondaryColor
            if (highlighted) Theme.highlightColor;
            else if (model.status === "Ended") Theme.errorColor;
            else if (model.status === "Running" && next !== "?") Theme.highlightColor
            else if (model.status === "Running" && next === "?") Theme.primaryColor
            else Theme.secondaryColor
         }
         wrapMode: Text.WordWrap
         anchors.topMargin: Theme.paddingMedium
         anchors.bottom: showP.top
         //anchors.left: showImg.right
         anchors.left: showS.right
         anchors.right: parent.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
         font.bold: true
     }

    Text {
         id: showP
         text: "Premiered:"
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.WordWrap
         anchors.bottom: showR.top
         anchors.left: showImg.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
     }

    Text {
         id: showPrem
         text: model.premiered !== null ? model.premiered : ""
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.WordWrap
         anchors.bottom: showR.top
         anchors.left: showP.right
         anchors.right: parent.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
         font.bold: true
     }

/*    Text {
         id: showPrem
         text: model.premiered !== null ? "Premiered: <b>" + model.premiered + "</b>" : ""
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.WordWrap
         anchors.top: showStatus.bottom
         anchors.left: showImg.right
         anchors.right: parent.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
     }
*/

    Text {
         id: showR
         text: "Average rating:"
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.WordWrap
         anchors.bottom: showN.top
         anchors.left: showImg.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
     }

    Text {
         id: showRating
         text: model.rating.average ? model.rating.average : "?"
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.WordWrap
         anchors.bottom: showN.top
         anchors.left: showR.right
         anchors.right: parent.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
         font.bold: true
     }
/*
    Text {
         id: showRating
         text: model.rating.average ? "Average rating: <b>" + model.rating.average + "</b>" : "Average rating: <b>?</b>"
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.WordWrap
         anchors.top: showPrem.bottom
         anchors.left: showImg.right
         anchors.right: parent.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
     }
*/
    Text {
         id: showN
         text: "Network:"
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.ElideRight
         maximumLineCount: 1
         anchors.bottom: showPr.top
         anchors.left: showImg.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
     }

    Text {
         id: showNetwork
         text: model.webChannel ? model.webChannel.name : model.network.name
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.ElideRight
         maximumLineCount: 1
         anchors.bottom: showPr.top
         anchors.left: showN.right
         anchors.right: parent.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
         font.bold: true
     }
/*
    Text {
         id: showNetwork
         text: model.webChannel ? "Network: <b>" + model.webChannel.name + "</b>" : "Network: <b>" + model.network.name + "</b>"
         //enabled: model.network.name ? true : false
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.ElideRight
         maximumLineCount: 1
         anchors.top: showRating.bottom
         anchors.left: showImg.right
         anchors.right: parent.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
     }
*/
    Text {
         id: showPr
         text: "Previous:"
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.ElideRight
         maximumLineCount: 1
         anchors.bottom: showNe.top
         anchors.left: showImg.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
     }

    Text {
         id: showPrev
         text: prev
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.ElideRight
         maximumLineCount: 1
         anchors.bottom: showNe.top
         anchors.left: showPr.right
         anchors.right: parent.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
         font.bold: prev !== "Unknown" ? true : false
     }

    Text {
         id: showNe
         text: nextse !== "" ? "Next: ("+nextse+")" : "Next: "
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.ElideRight
         maximumLineCount: 1
         anchors.bottom: showImg.bottom
         anchors.bottomMargin: Theme.paddingSmall
         anchors.left: showImg.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
     }

    Text {
         id: showNext
         text: next
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.ElideRight
         maximumLineCount: 1
         anchors.bottom: showImg.bottom
         anchors.bottomMargin: Theme.paddingSmall
         anchors.left: showNe.right
         anchors.right: parent.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
         font.bold: next !== "Unknown" ? true : false
     }

 /*   Text {
         id: showGenre
         text: ""//sgenres.get(1)[0]//genre()  //model.network ? "Network: <b>" + model.network.name + "</b>" : "Network:"
         color: highlighted ? Theme.highlightColor : Theme.primaryColor
         wrapMode: Text.ElideRight
         maximumLineCount: 1
         anchors.top: showImg.bottom
         anchors.left: parent.left
         anchors.right: parent.right
         anchors.leftMargin: Theme.paddingMedium
         font.pixelSize: Theme.fontSizeSmall
     } */

    onPressAndHold: {
        pageStack.push(Qt.resolvedUrl("../pages/ShowImg.qml"),{"img": model.image ? model.image.medium : "../No-Image-.png"})
        //pageStack.push(Qt.resolvedUrl("SeasonsPage.qml"),{"showid": model.id,"showname": model.name,"showimage": showImg.source} )
    }

    onClicked: {
        //frompage  === undefined ?
        var network
        model.webChannel ? network = model.webChannel.name  : network = model.network.name
        pageStack.push(Qt.resolvedUrl("../pages/InfoPage.qml"),
                                    {"showid": model.id,"showname": model.name, "showimage": showImg.source, "summary": model.summary, "showstatus": model.status, "showprem": model.premiered, "showrating": model.rating.average, "shownetwork": network, "showupdated": model.updated, "showprev": model.previousepisode, "shownext": model.nextepisode, "simdb": model.externals.imdb, "sofficial": model.officialSite})


        //:
        //pageStack.pop(frompage,PageStackAction.Immediate)
        //frompage = undefined
        //pageStack.push(Qt.resolvedUrl("../pages/InfoPage.qml"),{"showid": model.id,"showname": model.name, "showimage": showImg.source, "summary": model.summary, "showstatus": model.status, "showprem": model.premiered, "showrating": model.rating.average, "shownetwork": model.network.name, "showupdated": model.updated, "showprev": model.previousepisode, "shownext": model.nextepisode})//pageStack.clear();pageStack.push(Qt.resolvedUrl("SearchPage.qml")),{};
    }
}