package ru.stqa.ptf.mantis.tests;

import biz.futureware.mantis.rpc.soap.client.IssueData;
import biz.futureware.mantis.rpc.soap.client.MantisConnectLocator;
import biz.futureware.mantis.rpc.soap.client.MantisConnectPortType;
import org.openqa.selenium.remote.BrowserType;
import org.testng.SkipException;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import ru.stqa.ptf.mantis.appmanager.ApplicationManager;
import ru.stqa.ptf.mantis.model.Issue;
import ru.stqa.ptf.mantis.appmanager.SoapHelper;

import javax.xml.rpc.ServiceException;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;


public class TestBase {

     protected static final ApplicationManager app =
             new ApplicationManager(System.getProperty("browser",BrowserType.CHROME));
              // new available only with constructor in AppMng !!!


    @BeforeSuite
    public void setUp() throws Exception {
        //app.init();
        //app.ftp().upload(new File("src/test/resources/config_inc.php"), "config_inc.php", "config_inc.php.bak");
    }

    @AfterSuite(alwaysRun = true)
    public void tearDown() throws IOException {
        //app.stop();
        //app.ftp().restore( "config_inc.php.bak", "config_inc.php");
    }


    public void skipIfNotFixed(int issueID) throws IOException, ServiceException,SkipException {
        if(isIssueOpen(issueID)) {
            throw new SkipException("Ignored because of issue " +issueID);
        }
    }

    public boolean isIssueOpen(int issueId) throws IOException, ServiceException {
             //return !getIssueById(issueId).getState_name().equals("Resolved");
        return !getIssueById(issueId);
    }

    public Boolean getIssueById(int id) throws IOException, ServiceException {
        MantisConnectPortType mc = getMantisConnect2();
        BigInteger idBig = BigInteger.valueOf(id);
        IssueData createdIssueData = mc.mc_issue_get("administrator", "root", idBig);
            System.out.println("Project id: "+id +" in Status: " +
                    createdIssueData.getStatus().getName());
        return  createdIssueData.getStatus().getName().equals("Resolved");
        }

    private MantisConnectPortType getMantisConnect2() throws ServiceException, MalformedURLException {
        return new MantisConnectLocator().getMantisConnectPort(new URL(
                "http://localhost/mantisbt-1.3.0/api/soap/mantisconnect.php"));
    }
}

@@ -528,16 +528,57 @@
 pHelper;
+%0Aimport ru.stqa.ptf.mantis.model.Project;
 %0A%0Aimport
@@ -748,16 +748,61 @@
 et.URL;%0A
+import java.util.List;%0Aimport java.util.Set;%0A
 %0A%0Apublic
@@ -2043,16 +2043,729 @@
         
+    IssueData%5B%5D issues = mc.mc_project_get_issues(%22administrator%22, %22root%22,%0A                    BigInteger.valueOf(1),BigInteger.valueOf(1),BigInteger.valueOf(12));%0A                    //idBig,BigInteger.valueOf(1),BigInteger.valueOf(4));%0A        System.out.println(%22issues list for Project ' 1  ' :%22);%0A        for (IssueData i: issues) %7B%0A            System.out.println(i.getId() +%22 %22+ i.getSummary() +%22 in Status %22+ i.getStatus().getName());%0A            if (i.getId().equals(idBig)) %7B%0A                if(i.getStatus().getName().equals(%22Resolved%22)) %7B%0A                    return true;%0A                %7D else %7B%0A                    return false;%0A                %7D%0A            %7D%0A        %7D%0A    return  false;%0A        //
 IssueDat
@@ -2834,32 +2834,34 @@
 idBig);%0A        
+//
     System.out.p
@@ -2903,32 +2903,34 @@
 us: %22 +%0A        
+//
             crea
@@ -2970,24 +2970,26 @@
 ));%0A        
+//
 return  crea
@@ -3039,28 +3039,24 @@
 Resolved%22);%0A
-    
     %7D%0A%0A    p

add home task21 with list of issues